Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 9183 | Accepted: 4313 |
Description
Input
Output
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 }
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 }
poj 2531 -- Network Saboteur,布布扣,bubuko.com
原文:http://www.cnblogs.com/ubuntu-kevin/p/3892910.html