Network Saboteur
Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 31 Accepted Submission(s) : 12
Problem 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). <br>Output file must contain a single integer -- the maximum traffic between the subnetworks. <br>
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
题意:
有n个节点,两两之间有个权值,要求找出一种分配方法将n个点分为两份,使得两组点间总权值最大
解题思路:
比较特别的一种深搜方法,题意很简单,但是思路却不容易捋清楚
代码:
#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<queue>
#include<cstring>
using namespace std;
int cnt,n,a[21][21];
bool ok[21];
void dfs(int i,int sum) //sum是i点加入第一组之前,两组间的权值
{
ok[i]=true;
int num=sum;
for(int j=1;j<=n;j++)
{
if(ok[j])
num-=a[i][j]; //i新加入第一组,j是第一组的,那么原本i,j是不同组两者有权值,但是i加入后num中要去掉两者的权值
else
num+=a[i][j]; //j是第二组,i,j原本相同组,i加入第一组,两者变成不同组,所以要在num中加上两者的权值
}
if(cnt<num) cnt=num; //对于不同分配,跟新最大值
if(num>sum) //如果i加入第一组后num值变小了,就抛弃i加入第一组的情况
{
for(int k=i+1;k<=n;k++) //在i加入第一组情况下,处理其它节点
{
dfs(k,num); //假设k点加入
ok[k]=false; //k点不加入
}
}
}
int main()
{
scanf("%d",&n);
int i,j;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
cnt=-1000000;
memset(ok,false,sizeof(ok));
dfs(1,0); //第一个可以直接放入,因为分两组,组并不是固定的,加入第一个才算开始分组,
//第一个放入算是初始化,也就是把第一个先放入哪一组,哪组就是第一组
printf("%d\n",cnt);
return 0;
}