Network Saboteur
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 7751 | Accepted: 3474 |
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).
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 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
3 0 50 30 50 0 40 30 40 0
Sample Output
90
Source
Northeastern Europe 2002, Far-Eastern Subregion
一开始被这题唬住了,第一种:用dfs搜一下就行,要用一下优化否则会超时
#include <stdio.h>
#include <string.h>
#include <math.h>
int a[30][30],status[30];
int b[30],top;
int n,sum,max;
int main()
{
void dfs(int x);
int i,j,m,s,t;
scanf("%d",&n);
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
scanf("%d",&a[i][j]);
}
}
max=0;
for(sum=1;sum<=n/2;sum++)
{
memset(status,0,sizeof(status));
top=0;
dfs(0);
}
printf("%d\n",max);
return 0;
}
void dfs(int x)
{
int i,j,s,u;
if(x==n)
{
return ;
}
for(i=0;i<=1;i++)
{
if(i==1)
{
b[top++]=x;
status[x]=1;
}
if(top==sum)
{
for(j=0,s=0;j<=top-1;j++)
{
for(u=0;u<=n-1;u++)
{
if(!status[u])
{
s+=a[b[j]][u];
}
}
}
if(s>max)
{
max=s;
}
}else
{
dfs(x+1);
}
if(i==1)
{
status[x]=0;
top--;
}
}
}
第二种: 位运算
#include <stdio.h>
#include <string.h>
#include <math.h>
int a[30][30];
int b[30],status[30];
int main()
{
int i,j,n,m,s,t,top,max,x;
scanf("%d",&n);
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
scanf("%d",&a[i][j]);
}
}
max=0;
for(i=1;i< 1<<n; i++)
{
top=0;
memset(status,0,sizeof(status));
for(j=0;j<=n-1;j++)
{
if(i&1<<j)
{
status[j]=1;
b[top++]=j;
}
}
for(j=0,s=0;j<=top-1;j++)
{
for(x=0;x<=n-1;x++)
{
if(!status[x])
{
s+=a[b[j]][x];
}
}
}
if(s>max)
{
max=s;
}
}
printf("%d\n",max);
}