首页 > 其他 > 详细

[回溯]KTV uva11218

时间:2014-02-12 21:45:16      阅读:335      评论:0      收藏:0      [点我收藏+]

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.

bubuko.com,布布扣

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 abc,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

Output for the Sample Input

Case 1: 6
Case 2: -1

Rujia Liu‘s Present 2: A Big Contest of Brute Force


一道非常简单的回溯搜索题目。

题意:最近有一首三人合唱的歌很流行,你与朋友共九个人一同到KTV欢唱,你们决定一人只能唱一次,也就是将九个人分成三组,一组三人,每人刚好都被分派到一个组别。但是有些人并不喜欢与另一些人搭档,而有些组合的效果并不好听,所以我们对所有可能的三人组合打分数,请找出9人最高的分组分数总和。


 

#include<iostream>
#include<cstring>

using namespace std;

int vis[15],arry[100];
int cost,tag,m;

class State
{
public:
    int x,y,z,val;
}st[100];

bool ok()
{
    for(int i=1;i<=9;i++)
        if(!vis[i]) return false;
    return true;
}

void dfs(int pos,int value)
{
    if(ok())
    {
        tag=1;
        if(value>cost) cost=value;
        return;
    }
    int i,j,k;
    for(i=0;i<m;i++)
    {
        if(arry[i]==0&&vis[st[i].x]==0&&vis[st[i].y]==0&&vis[st[i].z]==0)
        {
            arry[i]=1;
            vis[st[i].x]=vis[st[i].y]=vis[st[i].z]=1;
            dfs(i,value+st[i].val);
            arry[i]=0;
            vis[st[i].x]=vis[st[i].y]=vis[st[i].z]=0;
        }
    }
}

int main()
{
    int k=0;
    while(cin>>m&&m)
    {
        tag=0,cost=0;
        memset(vis,0,sizeof(vis));
        memset(arry,0,sizeof(arry));
        for(int i=0;i<m;i++)
            cin>>st[i].x>>st[i].y>>st[i].z>>st[i].val;
        for(int i=0;i<m;i++)
        {
            arry[i]=1;
            vis[st[i].x]=vis[st[i].y]=vis[st[i].z]=1;
            dfs(i,st[i].val);
            arry[i]=0;
            vis[st[i].x]=vis[st[i].y]=vis[st[i].z]=0;
        }
        cout<<"Case "<<++k<<": ";
        if(tag==0) cout<<"-1"<<endl;
        else cout<<cost<<endl;
    }
    return 0;
}

[回溯]KTV uva11218

原文:http://blog.csdn.net/zju_ziqin/article/details/19108101

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!