POJ 2531 Network Saboteur(暴力+小剪枝)

题目链接

想起了一句名言。。。只要常数卡的好,没有暴力过不了。裸暴力超时了,加上一个标记,时间优化一半,然后就1100+水过去了。

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cmath>
 4 #include <string>
 5 #include <map>
 6 #include <algorithm>
 7 #include <queue>
 8 #include <vector>
 9 using namespace std;
10 int p[21][21],o[21],flag[1<<20];
11 int main()
12 {
13     int n,i,j,k,ans,temp,sum;
14     scanf("%d",&n);
15     for(i = 1; i <= n; i ++)
16     {
17         for(j = 1; j <= n; j ++)
18             scanf("%d",&p[i][j]);
19     }
20     ans = 0;
21     for(i = 1; i < (1<<n); i ++)
22     {
23         if(!flag[i])
24         {
25             flag[i] = 1;
26             sum = 0;
27             for(j = 0; j < n; j ++)
28             {
29                 if(i&(1<<j))
30                     o[j] = 1;
31                 else
32                 {
33                     sum += (1<<j);
34                     o[j] = 0;
35                 }
36             }
37             flag[sum] = 1;
38             temp = 0;
39             for(j = 0; j < n; j ++)
40             {
41                 if(o[j])
42                 {
43                     for(k = 0; k < n; k ++)
44                     {
45                         if(!o[k])
46                             temp += p[j+1][k+1];
47                     }
48                 }
49             }
50             if(temp > ans)
51                 ans = temp;
52         }
53     }
54     printf("%d\n",ans);
55     return 0;
56 }

 

posted @ 2013-01-20 13:18  Naix_x  阅读(195)  评论(0编辑  收藏  举报