Network Saboteur
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 12201 | Accepted: 5878 |
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
题目意思:给出n个点,输入Cij表示从点i到点j的值为Cij.现要要把n个点分成两部分A集合,B集合,求A中的每个点到B中的每个点的总和值最大。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
int Map[25][25], Max, n;
bool dis[25];
void Dfs(int x, int sum)
{
dis[x] = true;
for(int i = 0; i < n; i++)
{
if(dis[i] == 1)
sum -= Map[x][i];
else
sum += Map[x][i];
}
Max = max(sum, Max);
for(int i = x + 1; i < n; i++)
{
dis[i] = true;
Dfs(i, sum);
dis[i] = false;
}
}
int main()
{
while(~scanf("%d", &n))
{
int i, j;
memset(Map, 0, sizeof(Map));
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
scanf("%d", &Map[i][j]);
}
}
Max = 0;
Dfs(0, 0);
cout<<Max<<endl;
}
return 0;
}