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?
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
Case 1: 6 Case 2: -1#include <string.h> #include <stdio.h> struct node {int a,b,c,score; }s[100]; int n,visit[10],max; void dfs(int num,int step,int sum) { if (step>3) {if (sum>max) max=sum; return ;} if (num>n) return ; if (visit[s[num].a]+visit[s[num].b]+visit[s[num].c]==0) {visit[s[num].a]=1; visit[s[num].b]=1; visit[s[num].c]=1; dfs(num+1,step+1,sum+s[num].score); visit[s[num].a]=0; visit[s[num].b]=0; visit[s[num].c]=0; } dfs(num+1,step,sum); }; int main() {int i,Case=0; while (scanf("%d",&n),n) { memset(visit,0,sizeof(visit)); max=-1; ++Case; for (i=1;i<=n;i++) scanf("%d%d%d%d",&s[i].a,&s[i].b,&s[i].c,&s[i].score); dfs(1,1,0); printf("Case %d: %d\n",Case,max); } return 0; }