Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 12304 | Accepted: 5938 |
Description
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
Output file must contain a single integer -- the maximum traffic between the subnetworks.
Output
Sample Input
3 0 50 30 50 0 40 30 40 0
Sample Output
题目意思:给出n个点,输入Cij表示从点i到点j的值为Cij.现要要把n个点分成两部分A集合,B集合,求A中的每个点到B中的每个点的总和值最大。
分析:假设有10个点,点i位置为1则在A集合,为0在B集合。
位置 1 2 3 4 5 6 7 8 9 10
第一种 1 1 1 1 1 1 1 1 1 0
1 1 1 1 1 1 1 1 0 1
1 1 1 1 1 1 1 1 0 0
..........................................
最后一种: 0 0 0 0 0 0 0 0 0 0
思路:DFS思路:
1、把这两个集合标记为0和1,先默认所有点都在集合0里。
2、依次枚举每个点id,把每个点都放到集合1里去,这个时候就要调整集合的权值了,原来和id都在集合0里的点,要把权值加上;而在集合1里的点,要把权值减去。
3、权值调整完毕后,和ans比较,如果比ans要大, 调整ans。
4、如果放到集合1中,调整节点后的权值比放在集合0中要大,那么就默认这个点在集合1中,继续枚举下面的点进行DFS。最终是可以把最有状态都枚举出来的。
#include <iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include <algorithm>
using namespace std;
int p[25][25],vis[25];
int n,res;
void dfs(int e,int sum)
{
int i,num;
num=sum;
vis[e]=1;
for(i=0; i<n; i++)
{
if(vis[i])
num-=p[e][i];
else num+=p[e][i];
}
res=max(num,res);
for(i=e+1; i<n; i++)
if(sum<num)
{
dfs(i,num);
vis[i]=0;
}
}
int main()
{
int i,j;
while(~scanf("%d",&n))
{
memset(p,0,sizeof(vis));
for(i=0; i<n; i++)
for(j=0; j<n; j++)
scanf("%d",&p[i][j]);
res=0;
memset(vis,0,sizeof(vis));
dfs(0,0);
printf("%d\n",res);
}
}