POJ2531 Network Saboteur
题目内容
Network Saboteur
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 6562 Accepted: 2844
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
3
0 50 30
50 0 40
30 40 0
Sample Output
90
Source
Northeastern Europe 2002, Far-Eastern Subregion
大意:题目给出一个完全图,将此图分为两部分使得连接两部分边的权和最大。
直接DFS即可,代码中有详细注释。
View Code
1 #include<cstdio>
2 #include<cstring>
3 const long MAXN=20;
4 long N,g[MAXN][MAXN]; //g存储邻接矩阵
5 long ans;
6 short group[MAXN]; //存放分组信息,group[x]=1意为第x个点分组为1
7 void init()
8 {
9 scanf("%ld",&N);
10 for(long i=0;i!=N;i++)
11 for(long j=0;j!=N;j++)
12 scanf("%ld",&g[i][j]);
13 memset(group,0,sizeof(group)); //所有点均未分组
14 ans=0;
15 }
16 void dfs(long index,long sum) //index:第x个点(0下标开始),sum:相加权数
17 {
18 if(index==N) //搜索完所有点,更新答案
19 {
20 if(ans<sum)
21 ans=sum;
22 return;
23 }
24 long t=0;
25 for(long i=0;i!=index;i++)
26 if(group[i]==2) //扫描当前点之前的所有点,若为2组则相加权值
27 t+=g[index][i];
28 group[index]=1; //把当前点分到1组
29 dfs(index+1,sum+t); //进一步搜索
30 t=0; //再扫描1组的情况
31 for(long i=0;i!=index;i++)
32 if(group[i]==1)
33 t+=g[index][i];
34 group[index]=2;
35 dfs(index+1,sum+t);
36 }
37 int main(void)
38 {
39 init();
40 group[0]=1; //将第一个点分到1组
41 dfs(1,0);
42 printf("%ld\n",ans);
43 return 0;
44 }
posted on 2011-10-31 18:12 SilVeRyELF 阅读(366) 评论(0) 收藏 举报

浙公网安备 33010602011771号