poj 2531 -- Network Saboteur

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 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).

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

3
0 50 30
50 0 40
30 40 0

Sample Output

90

原题链接:Network Saboteur

思路:枚举集合的个数,算出最大流量。DFS.说是减枝题。不知道怎么减。跑了141ms,改了一下子。跑了94ms。

141ms code:
 1 /*======================================================================
 2  *           Author :   kevin
 3  *         Filename :   NetworkSaboteur.cpp
 4  *       Creat time :   2014-08-05 16:11
 5  *      Description :
 6 ========================================================================*/
 7 #include <iostream>
 8 #include <algorithm>
 9 #include <cstdio>
10 #include <cstring>
11 #include <queue>
12 #include <cmath>
13 #define clr(a,b) memset(a,b,sizeof(a))
14 #define M 25
15 using namespace std;
16 int s[M][M];
17 int n,vis[M],ans;
18 void DFS(int id,int sum)
19 {
20     for(int i = id; i < n; i++){
21         vis[i] = 1;
22         int temp = sum;
23         for(int j = 1; j <= n; j++){
24             if(!vis[j]){
25                 temp += s[i][j];
26             }
27             else{
28                 temp -= s[i][j];
29             }
30         }
31         if(ans < temp){
32             ans = temp;
33         }
34         DFS(i+1,temp);
35         vis[i] = 0;
36     }
37 }
38 int main(int argc,char *argv[])
39 {
40     while(scanf("%d",&n)!=EOF){
41         clr(s,0);
42         clr(vis,0);
43         for(int i = 1; i <= n; i++){
44             for(int j = 1; j <= n; j++){
45                 scanf("%d",&s[i][j]);
46             }
47         }
48         ans = 0;
49         DFS(1,0);
50         printf("%d\n",ans);
51     }
52     return 0;
53 }
View Code
94ms code:
 1 /*======================================================================
 2  *           Author :   kevin
 3  *         Filename :   NetworkSaboteur.cpp
 4  *       Creat time :   2014-08-05 16:11
 5  *      Description :
 6 ========================================================================*/
 7 #include <iostream>
 8 #include <algorithm>
 9 #include <cstdio>
10 #include <cstring>
11 #include <queue>
12 #include <cmath>
13 #define clr(a,b) memset(a,b,sizeof(a))
14 #define M 25
15 using namespace std;
16 int s[M][M];
17 int n,vis[M],ans;
18 void DFS(int id,int sum,int first)
19 {
20     for(int i = id; i <= n; i++){
21         vis[i] = 1;
22         int temp = sum;
23         for(int j = 1; j <= n; j++){
24             if(!vis[j]){
25                 temp += s[i][j];
26             }
27             else{
28                 temp -= s[i][j];
29             }
30         }
31         if(ans < temp){
32             ans = temp;
33         }
34         if(i+1 <= n)
35             DFS(i+1,temp,0);
36         vis[i] = 0;
37         if(first) break;
38     }
39 }
40 int main(int argc,char *argv[])
41 {
42     while(scanf("%d",&n)!=EOF){
43         clr(s,0);
44         clr(vis,0);
45         for(int i = 1; i <= n; i++){
46             for(int j = 1; j <= n; j++){
47                 scanf("%d",&s[i][j]);
48             }
49         }
50         ans = 0;
51         DFS(1,0,1);
52         printf("%d\n",ans);
53     }
54     return 0;
55 }
View Code

 

poj 2531 -- Network Saboteur,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。