DFS/POJ 2531 Network Saboteur

 1 #include<cstdio>
 2 #include<cstring>
 3 using namespace std;
 4 int n,ans;
 5 int a[30][30],f[30];
 6 int cmax(int a,int b){return a>b?a:b;}
 7 void dfs(int x,int now)
 8 {
 9     if (x>n)
10     {
11         ans=cmax(ans,now);
12         return;
13     }
14     int temp=0;
15     f[x]=1;
16     for (int i=1;i<x;i++)
17         if (f[i]==0) temp+=a[x][i];
18     dfs(x+1,now+temp);
19
20     temp=0;
21     f[x]=0;
22     for (int i=1;i<x;i++)
23         if (f[i]==1) temp+=a[x][i];
24     dfs(x+1,now+temp);
25 }
26 int main()
27 {
28     scanf("%d",&n);
29     memset(f,0,sizeof(f));
30     ans=0;
31     for (int i=1;i<=n;i++)
32         for (int j=1;j<=n;j++) scanf("%d",&a[i][j]);
33     dfs(1,0);
34     printf("%d\n",ans);
35     return 0;
36 }
时间: 2024-10-08 16:04:45

DFS/POJ 2531 Network Saboteur的相关文章

poj 2531 Network Saboteur 解题报告

题目链接:http://poj.org/problem?id=2531 题目意思:将 n 个点分成两个部分A和B(也就是两个子集啦), 使得子集和最大(一定很难理解吧,呵呵).举个例子吧,对于样例,最佳的分法就是把点2分为一个子集,另一个子集理所当然就是1.3了. 2-1 的权值是50,2-3的权值是40,那么最大就是50+40 = 90了. 首先dfs的话,我不太会做啦.看了队长的用了状态压缩来做,一下子觉得好神奇!!!! 可能第一次接触,理解得不是太深刻,先留着吧.就觉得好神奇,好神奇...

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 di

POJ 2531 Network Saboteur(dfs)

题目代号:POJ 2531 题目链接:http://poj.org/problem?id=2531 Language: Default Network Saboteur Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13290   Accepted: 6428 Description A university network is composed of N computers. System administrator

poj 2531 Network Saboteur (dfs)

Network Saboteur Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9364   Accepted: 4417 Description A university network is composed of N computers. System administrators gathered information on the traffic between nodes, and carefully di

POJ 2351 Network Saboteur

Network Saboteur Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14087   Accepted: 6893 Description A university network is composed of N computers. System administrators gathered information on the traffic between nodes, and carefully d

PKU 2531 Network Saboteur(dfs+剪枝||随机化算法)

题目大意:原题链接 给定n个节点,任意两个节点之间有权值,把这n个节点分成A,B两个集合,使得A集合中的每一节点与B集合中的每一节点两两结合(即有|A|*|B|种结合方式)权值之和最大. 标记:A集合:true  B集合:false 解法一:dfs+剪枝 #include<iostream> #include<cstring> using namespace std; int n,ans; bool in[25]; int graph[25][25]; void dfs(int i

【POJ 2531】Network Saboteur

[POJ 2531]Network Saboteur 图的搜索 剪枝真是门学问..剪好了快的可真不是一倍两倍 刚开始搜的思路有问题 TLE了 后来枚举点暴力搜了一发 两百多ms 由于查找时权值是不断增加的 所以直接找集合间最大权的话不方便设置return点 看disscuss发现有一大牛 建了两个数组 通过所有边权-两集合内部边权(去重) 得到答案 dfs的时候找最小内部边权即可 当前状态权值>当前最小内部边权时直接跳出 两个数组分别寸当前状态两个集合中所含的点 每加一个点分别往两边加 假设要将

poj 2531 搜索剪枝

Network Saboteur Time Limit: 2000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u Java class name: Main [Submit] [Status] [Discuss] Description A university network is composed of N computers. System administrators gathered informat

POJ 2531 暴力深搜

Network Saboteur Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13494   Accepted: 6543 Description A university network is composed of N computers. System administrators gathered information on the traffic between nodes, and carefully d