Network saboteur
Time Limit: 2000MS |
|
Memory Limit: 65536K |
Total Submissions: 9183 |
|
Accepted: 4313 |
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
Link to the original topic: Network saboteur
Idea: Enumerate the number of collections and figure out the maximum flow. DFS. is said to be a subtraction problem. I don't know how to reduce it. Ran 141ms, changed all of a sudden. Ran the 94ms.
141ms Code:
1 /*======================================================================2 * Author:kevin3 * Filename:NetworkSaboteur.cpp4 * creat time:2014-08-05 16:115 * Description:6 ========================================================================*/7#include <iostream>8#include <algorithm>9#include <cstdio>Ten#include <cstring> One#include <queue> A#include <cmath> - #defineCLR (A, B) memset (A,b,sizeof (a)) - #defineM 25 the using namespacestd; - intS[m][m]; - intN,vis[m],ans; - voidDFS (intIdintsum) + { - for(inti = ID; I < n; i++){ +Vis[i] =1; A inttemp =sum; at for(intj =1; J <= N; J + +){ - if(!Vis[j]) { -Temp + =S[i][j]; - } - Else{ -Temp-=S[i][j]; in } - } to if(Ans <temp) { +Ans =temp; - } theDFS (i+1, temp); *Vis[i] =0; $ }Panax Notoginseng } - intMainintargcChar*argv[]) the { + while(SCANF ("%d", &n)! =EOF) { ACLR (S,0); theCLR (Vis,0); + for(inti =1; I <= N; i++){ - for(intj =1; J <= N; J + +){ $scanf"%d",&s[i][j]); $ } - } -Ans =0; theDFS (1,0); -printf"%d\n", ans);Wuyi } the return 0; -}
View Code
94ms Code:
1 /*======================================================================2 * Author:kevin3 * Filename:NetworkSaboteur.cpp4 * creat time:2014-08-05 16:115 * Description:6 ========================================================================*/7#include <iostream>8#include <algorithm>9#include <cstdio>Ten#include <cstring> One#include <queue> A#include <cmath> - #defineCLR (A, B) memset (A,b,sizeof (a)) - #defineM 25 the using namespacestd; - intS[m][m]; - intN,vis[m],ans; - voidDFS (intIdintSumintFirst ) + { - for(inti = ID; I <= N; i++){ +Vis[i] =1; A inttemp =sum; at for(intj =1; J <= N; J + +){ - if(!Vis[j]) { -Temp + =S[i][j]; - } - Else{ -Temp-=S[i][j]; in } - } to if(Ans <temp) { +Ans =temp; - } the if(i+1<=N) *DFS (i+1, temp,0); $Vis[i] =0;Panax Notoginseng if(first) Break; - } the } + intMainintargcChar*argv[]) A { the while(SCANF ("%d", &n)! =EOF) { +CLR (S,0); -CLR (Vis,0); $ for(inti =1; I <= N; i++){ $ for(intj =1; J <= N; J + +){ -scanf"%d",&s[i][j]); - } the } -Ans =0;WuyiDFS (1,0,1); theprintf"%d\n", ans); - } Wu return 0; -}
View Code