uvaoj11218KTV

uvaoj11218

      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 integer n
(0 < n < 81), the number of possible combinations. The next n lines each contains 4 positive integers
a, b, c, s (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
Sample Output
Case 1: 6
Case 2: -1

---------------------------------------------------------------------------------------------------------------------------------------------------------

题目意思:

      我看了好长时间,才懂题目的意思 。。  就是,输入一个数,这个数表示  能够结合为一组的可能数,下面输入是各种组合情况 ( the number of possible combinations)。只能在你输入的这些可能组合中选三组(人不重复),第四位是组合在一起的分数,求三个组合的总分数最大。(不同的人组合在一起,可能关系好组合在一起分数就高,每个人发挥可能水平就不同了,所以一个人的得分是不固定的,要看组合整体)

    

      所以,在这里用了枚举+dfs 

      这个题比较简单。关键是看懂题目目的。

----------------------------------------------------------------------------------------------------------------------------------------------------------


#include<stdio.h>
#include<string.h>
int st[1000][4];
int vis[10];
int Vis[1000];
int sum ;
int count;
void dfs(int n , int c , int s)
{
	int i;
	
	if(n == c)
	{
		sum =  sum<s?s:sum;
	}
	else for(i=0;i<count;i++)
	{
		if(Vis[i] == 0 && vis[st[i][0]]==0 && vis[st[i][1]] == 0 && vis[st[i][2]]==0)
		{   
			
			Vis[i] = 1;
			vis[st[i][0]]=1 ; vis[st[i][1]] = 1 ; vis[st[i][2]]=1;
			dfs(n,c+1,st[i][3]+s);
			Vis[i] = 0;
			vis[st[i][0]]=0 ; vis[st[i][1]] = 0 ; vis[st[i][2]]=0;
		}
	}
}


int main()
{
	int i,n=0;
	while(scanf("%d",&count))
	{
		
		if(count == 0)break;

		for(i=0 ; i<count ; i++)
		scanf("%d %d %d %d",&st[i][0],&st[i][1],&st[i][2],&st[i][3]);

		memset(vis,0,sizeof(vis));
        memset(Vis,0,sizeof(Vis));
		sum = -1;
		dfs(3,0,0);

		printf("Case %d: %d\n",++n, sum);
	}


	return 0;
}


     

你可能感兴趣的:(枚举,c,ACM,DFS,ACM基础)