CO-ABILITY DFS+DP

本文介绍了一个算法问题,旨在通过合理的队员搭配实现团队合作能力的最大化。给定一组人员及其两两之间的合作能力值,需要计算出最佳的两人小组分配方案,使得所有小组的合作能力之和最大。

 CO-ABILITY

Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 20   Accepted Submission(s) : 11
Font: Times New Roman | Verdana | Georgia
Font Size: ← →

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
代码:

#include<stdio.h>
#include<string.h>
#define N 15
#define MX(a,b) (a>b?a:b)
int map[N][N];
int visit[N];
int p[N],pos[N];
int n;
int max;

void REDFS(int cur,int cnt)
{
  if(cnt==n/2)
  {
     int sum=0;
  for(int i=1;i<=cnt;i++)
  sum+=map[p[i]][pos[i]];
  max=MX(max,sum);
  return ;     
  }  
  for(int i=1;i<=n;i++)
  {
     if(!visit[i])
     {
     visit[i]=1;
  pos[cnt+1]=i;
  REDFS(i,cnt+1);
  visit[i]=0;      
     }
  }
}

void DFS(int cur,int cnt)
{
   if(cnt==n/2)
   {
      REDFS( 0,0 );
   return ;     
   }  
   for(int i=cur+1;i<=n;i++)
   {
       if(!visit[i])
    {
       visit[i]=1;
    p[cnt+1]=i;
    DFS(i,cnt+1);
    visit[i]=0;    
       }    
   }
}

int main()             //分两次搜索,存放在两个不同的数组中
{
   while(scanf("%d",&n),n)
   {
      memset(visit,0,sizeof(visit));
   max=-1;
   for(int i=1;i<=n;i++)
   {
      for(int j=1;j<=n;j++)
   scanf("%d",&map[i][j]);    
      } 
   DFS( 0,0 );
   printf("%d\n",max);      
   }  
   return 0;
}

链接:http://acm.hdu.edu.cn/diy/contest_showproblem.php?cid=16638&pid=1006

转载于:https://www.cnblogs.com/hebozi/archive/2012/08/13/2635862.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值