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 <cstdio> #include <cstring> using namespace std; struct Node{ int a,b,c,s; }com[90]; int vis[10]; int n; int max; void dfs(int cur,int group,int sum) { if(group == 3) { if(sum > max) { max = sum; } return ; } if(cur == n) { return ; } if(!vis[com[cur].a] && !vis[com[cur].b] && !vis[com[cur].c]) { //如果满足条件回溯 vis[com[cur].a] = true; vis[com[cur].b] = true; vis[com[cur].c] = true; dfs(cur+1,group+1,sum + com[cur].s); vis[com[cur].a] = false; vis[com[cur].b] = false; vis[com[cur].c] = false; } dfs(cur+1,group,sum); //如果不满足条件,查找下一个组合 } int main() { int cas = 1; while(scanf("%d",&n) != EOF && n) { for(int i = 0; i < n; i++) { scanf("%d%d%d%d",&com[i].a,&com[i].b,&com[i].c,&com[i].s); } memset(vis,0,sizeof(vis)); max = -1; dfs(0,0,0); printf("Case %d: %d\n",cas++,max); } return 0; }