poj 2531 Network Saboteur

来源:互联网 发布:软件自动化研究生 编辑:程序博客网 时间:2024/06/05 19:23

Description

A university network is composed of N computers. System administrators gathered information on the traffic between nodes, and carefully divided the network into two subnetworks in order to minimize traffic between parts. 
A disgruntled computer science student Vasya, after being expelled from the university, decided to have his revenge. He hacked into the university network and decided to reassign computers to maximize the traffic between two subnetworks. 
Unfortunately, he found that calculating such worst subdivision is one of those problems he, being a student, failed to solve. So he asks you, a more successful CS student, to help him. 
The traffic data are given in the form of matrix C, where Cij is the amount of data sent between ith and jth nodes (Cij = Cji, Cii = 0). The goal is to divide the network nodes into the two disjointed subsets A and B so as to maximize the sum ∑Cij (i∈A,j∈B).

Input

The first line of input contains a number of nodes N (2 <= N <= 20). The following N lines, containing N space-separated integers each, represent the traffic matrix C (0 <= Cij <= 10000). 
Output file must contain a single integer -- the maximum traffic between the subnetworks. 

Output

Output must contain a single integer -- the maximum traffic between the subnetworks.

Sample Input

30 50 3050 0 4030 40 0

Sample Output

90

//深搜,题目大意是有n台计算机,每两台之间有通信量,
//然后将这些计算机分成两部分,使得这两个部分间的通信量最大,同一部分之间的通信量不算
//解题思路:通过深搜将其分成两部分,每一次都计算两部分之间的通讯量,ans保存最大值 
// 个人认为重要的剪枝: 只考虑第一台放在左边的所有情况
//如:有四台计算机 为 1 2 3  
//则所有的分成两堆情况有 1    2 3;
// 1 2    3;
// 1 3    2;
#include<stdio.h>
#include<string.h>


int a[25][25];
int vis[25];
int n,ans;
int value[25];//记录同一边的值 


void dfs(int cur,int len)  // cur 代表当前的计算机,len 表示左边的一堆的数目 
{
if(len>=n) return;
// for(int i=1;i<=len;i++)
// printf(" %d ",value[i]);
// printf("\n"); 
int temp=0,temp2;

for(int i=1;i<=len;i++)
{
temp2=value[i]; 
for(int j=1;j<=n;j++)
{
if(vis[ j ])
continue;
temp += a[ temp2 ][ j ];
}

if(temp>ans)
ans=temp;
// printf("temp=%d\n",temp);

for(int i=1;i<=n;i++)
{
if(!vis[i])
{
if(i<cur) continue; // 如果找到放在这一堆里的计算机小于当前这一堆,
 // 则说明这种组合在前面已经进行过,这不用计算了 
value[len+1]=i;

vis[i]=1;
dfs(i,len+1);
vis[i]=0; 
}
}
}
int main()
{
while(~scanf("%d",&n))
{
ans=0;
memset(vis,0,sizeof(vis));

for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
scanf("%d",&a[i][j]);

// for(int i=1;i<=n;i++)不用考虑每种都放第一个的情况,只需考虑第一台 
// {
vis[1]=1;      
value[1]=1;
dfs(1,1);
vis[1]=0;
// }
printf("%d\n",ans);
}
return 0;
}

0 0
原创粉丝点击