Problem Description

The Regional Contest is coming near, and all the acmers become very excited. As well as our coaches, they're very strategy, for they always think again and again how to select players , how to choose three players into one team and which team to assign to one or two of all sites… Such problems make their heads ached, including your coach, and he knows you're an excellent ACMer, so he's asking for your help. This is an easy problem for you, isn't it?
To make things easier, we make a little change. Assume that your organization has N ACMers, they're numbered from 1 to N.(It has nothing to do with their ability, just for the convenience of controlling). What's more, they're divided into N/2 teams, haha, which means one team has only 2 people, and if your organization has 5 ACMers, you could have at most 2 teams. I hope you would like such changes.
Teamwork is very important, when different two ACMers are combined into one team, they can have different co-ability. And now, given any two ACMers: i and j, your coach knows their co-ability (Pij). Here comes the problem, given all the Pij(1 <= i <= N,1 <= j <= N,0 < Pij<= 100), you should tell the most co-ability after all ACMers has their team can your school has.

Input

The first line of the input is N(2<=N<=10 and N is an even number),the number of employees in the company.
Then there're N lines,each line has N numbers.The jth number in the ith line is Pij,as we discribe above.And we guarantee Pij = Pji,Pii = 0. The end-of-file is denoted by a single line containing the integer 0.

Output

For each case,output the most profits this company can make.

Sample Input

4  
0 6 62 13  
6 0 35 94  
62 35 0 5  
13 94 5 0  
0  

Sample Output

156

  题目大意就是没两个人一组,找出组合的最大值,若要是n为基数个人就取(n-1)/2组;直接利用深搜就可以了。。。
代码:
View Code
 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 int map[11][11],a[11],vist[11][11];
 5 int n,ans;
 6 
 7 int dfs(int cur)
 8 {
 9     int i,max;
10     if(cur==n)
11     {
12         memset(vist,0,sizeof(vist));
13         for(i=max=0;i<n;i++)
14             if(!vist[i][a[i]])//判断对应点是否用过 
15             {
16                 vist[i][a[i]]=vist[a[i]][i]=1;
17                 max+=map[i][a[i]];
18             }
19         if(max>ans)    ans=max;
20         return 1;
21     }
22     if(a[cur]==-1)
23     {
24         for(i=cur+1;i<n;i++)
25         {
26             if(a[i]==-1)
27             {
28                 a[cur]=i;
29                 a[i]=cur;  //  两点交互即确认对应点又可以起到去基功能 
30                 dfs(cur+1);
31                 a[cur]=a[i]=-1;
32             }
33         }
34     }
35     else
36         dfs(cur+1);
37     return 1;
38 }
39 
40 int main()
41 {
42     int i,j;
43     while(cin>>n&&n)
44     {
45         for(i=0;i<n;i++)
46             for(j=0;j<n;j++)
47                 cin>>map[i][j];
48         memset(a,-1,sizeof(a));
49         ans=0;
50         dfs(0);
51         cout<<ans<<endl;
52     }
53     return 0;
54 }

 

posted on 2012-08-13 11:00  xinmenghuairi  阅读(167)  评论(0编辑  收藏  举报