Network Saboteur
Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 27 Accepted Submission(s) : 11
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
Source
PKU
题意:表示题意太难懂,只是题意看了将近半小时。。。理解能力太差了!!给定n个点,以及它们之间的距离,将这些点分成两个集合,集合之间的距离为点之间的距离之和,求最大的距离。
思路:
首先我们将所有的店标记为0(即所有点放在一个集合里),然后取出一个site标记为1(即将该点放在另一个集合里),这是对于和site在一个集合里的点,我们减去他们两个之间的权值,对于不在一个集合里的点,我们加上他们之间的权值。最后的结果为最大值。
代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
using namespace std;
int map[40][40];
int tong[40];
int n,res;
void dfs(int site,int sum)
{
int i;
tong[site]=1;
int num=sum;
for(i=0;i<n;i++){
if(tong[i]==1) //与site在一个集合里的点
num-=map[site][i];//减去他们之间的权值
else
num+=map[site][i];//不在的加上
}
res=max(res,num);//依次循环结束找到的最大值
for(i=site+1;i<n;i++)//然后遍历剩下的点
{
if(num>sum){//小剪枝,如果加入了点site后两个集合之间的权值小了,则就不需要遍历这个点。加不加这个剪枝都可以通过
dfs(i,num);
tong[i]=0;
}
}
}
int main()
{
int i,j;
while(cin>>n)
{
memset(map,0,sizeof(map));
memset(tong,0,sizeof(tong));
for(i=0;i<n;i++)
for(j=0;j<n;j++)
cin>>map[i][j];
res=-1000000;
dfs(0,0);
cout<<res<<endl;
}
return 0;
}