uva 11218 - KTV

Problem K

KTV

One song is extremely popular recently, so you and your friends decided to sing it in KTV. The song has 3 characters, so exactly 3 people should sing together each time (yes, there are 3 microphones in the room). There are exactly 9 people, so you decided that each person sings exactly once. In other words, all the people are divided into 3 disjoint groups, so that every person is in exactly one group.

However, some people don't want to sing with some other people, and some combinations perform worse than others combinations. Given a score for every possible combination of 3 people, what is the largest possible score for all the 3 groups?

Input

The input consists of at most 1000 test cases. Each case begins with a line containing a single integern (0 < n < 81), the number of possible combinations. The next n lines each contains 4 positive integersabcs (1 <= a < b < c <= 9, 0 < s < 10000), that means a score of s is given to the combination (a,b,c). The last case is followed by a single zero, which should not be processed.

Output

For each test case, print the case number and the largest score. If it is impossible, print -1.

Sample Input

3
1 2 3 1
4 5 6 2
7 8 9 3
4
1 2 3 1
1 4 5 2
1 6 7 3
1 8 9 4
0

Output for the Sample Input

Case 1: 6
Case 2: -1

方法 :
直接 暴力枚舉。
代碼 :
#include 
#define N 81
int com[N][4];
int set[10];
void init_set()
{
    int i;
    for(i = 1; i < 10; i++)
	set[i] = 0;
}
int possible(int f, int s, int t)
{
    int i;
    init_set();
    for(i = 1; i < 4; i++){
	if(set[com[f][i]] == 1){
            return -1;
        }
        set[com[f][i]] = 1;
        if(set[com[s][i]] == 1){
            return -1;
        }
        set[com[s][i]] = 1; 
        if(set[com[t][i]] == 1){
	    return -1;
	}
        set[com[t][i]] = 1;
    }
    return 0;
}
int max_score(int n)
{
    int i, j, k, max, s;
    max = -1;
    for(i = 0; i < n; i++){
	for(j = i+1; j < n; j++){
	    for(k = j+1; k < n; k++){
		if(possible(i, j, k) >= 0){
		    s = com[i][0]+com[j][0]+com[k][0];
		    if(s > max)
			max = s;
		}
	    }
	}
    }
    return max;
}
int main()
{
    int n, c, k;
    c = 1;
    while(scanf("%d", &n) != EOF && n != 0){
	for(k = 0; k < n; k++){
	    scanf("%d %d %d %d", com[k]+1, com[k]+2, com[k]+3, com[k]);
	}
	printf("Case %d: %d\n", c, max_score(n));
        c++;
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章