Network saboteur
Time Limit: 2000MS |
|
Memory Limit: 65536K |
Total Submissions: 9544 |
|
Accepted: 4542 |
Description
A University Network is composed of N computers. System administrators gathered information on the traffic between nodes, and carefully divided the network into and the subnet Works in order to minimize traffic between parts.
A disgruntled computer science student Vasya, after being expelled from the university, decided to has his revenge. He hacked into the university network and decided to reassign computers to maximize the traffic between.
Unfortunately, he found, calculating such worst subdivision is one of the those problems he, being a student, failed to so Lve. So he asks-a more successful CS student, to-help him.
The traffic data is given in the form of Matrix C, where Cij is the amount of data sent between and ith jth (nodes = Cji, Cii = 0). The goal is to divide the network nodes into the and the 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
30 50 3050 0 4030 40 0
Sample Output
90
Test instructions: Give n points (0-n-1), the next n rows, each row has n number, line I represents the distance of the number of i-1 of the nth point, asked to divide these points into two parts, the two point sets between the maximum weight value. For example, the 0,1,2 is divided into two parts (1,3) and (2), the maximum distance is map[1][2]+map[3][2]=90.
Train of thought: How to think that this question and DFS have what relationship, know to see the solution of giant giant, suddenly, rose posture. First we mark all the stores as 0 (that is, all the points in a set), and then take out a site labeled 1 (which will be placed in another set), which is for the point in a collection with site, we subtract the weights between them, for the points not in a set, We add the weights between them. The final result is the maximum value.
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <iostream> #include < algorithm> #include <set> #include <queue> #include <stack>using namespace Std;const int inf= 0x3f3f3f3f;int map[40][40];int g[40];int n,res;void dfs (int site,int sum) {int i; g[site]=1;//take out point int num=sum; for (i=0;i<n;i++) {if (g[i]==1)//with site in a set of points num-=map[site][i];//minus the weights between them else//otherwise and site is not a collection The num+=map[site][i];//plus the weights between them} Res=max (res,num);//The maximum value found for (i=site+1;i<n;i++) {///And then loop over the remaining points if (num>sum) {//small pruning, if the weight between two sets is small after adding point site, then it is not necessary to traverse this point. DFS (I,num); g[i]=0; }}}int Main () {int i,j; while (~SCANF ("%d", &n)) {memset (map,0,sizeof (map)); memset (G,0,sizeof (g)); for (i=0;i<n;i++) for (j=0;j<n;j++) scanf ("%d", &map[i][j]); Res=-inf; DFS (0,0); printf ("%d\n", res); } return 0;}
POJ 2531-network saboteur (dfs+ pruning)