POJ 2531 dfs回溯(小剪枝)
来源:互联网 发布:淘宝无响应是怎么回事 编辑:程序博客网 时间:2024/06/05 18:15
Network Saboteur
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 10381 Accepted: 4988
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
题意:
有一个图,路径以数组矩阵表示,然后将所有点分到两个集合里,求分完后两个集合所有边的权重和。
解法:
开始非常直接的dfs+回溯,不到10min就写好水过了,但是看到有的人优化到了16ms,而粗鄙我的暴搜用了563ms(poj有些数据还是比较水的),就想到要剪枝,减枝有两点:1,到当前步时,把其后所有剩余的变权重加起来还不能比当前已经求得最大的MAX值大,则减去。2,可知,把第一个点放在A集合或者B集合会得到一样的两个结果(对称)。那么就只需要对放在A集合里讨论即可。如此优化到了94ms。
PS:实在不明白怎么优化到16ms的,还有神优化到了0ms。如果大家有想法,请赐教~~
1,直接dfs+回溯,比较直观。
#include <iostream>#include <algorithm>#include <cstdio>#include <queue>#include <cstring>using namespace std;int G[30][30];int MAX,n;int A[30],B[30];void dfs(int cur){ int i = 0,j = 0; if(cur == n){ int sum = 0; for(i = 0;A[i]!=-1;i++) for(j = 0;B[j]!=-1;j++) sum += G[A[i]][B[j]]; if(sum>MAX) MAX = sum; return ; } while(A[i++]!=-1);i--; while(B[j++]!=-1);j--; A[i] = cur; dfs(cur+1); A[i] = -1; B[j] = cur; dfs(cur+1); B[j] = -1;}int main(){ while(scanf("%d",&n)!=EOF){ int i,j; for(i = 0;i<n;i++) for(j = 0;j<n;j++) scanf("%d",&G[i][j]); MAX = -0x3f3f3f3f; memset(A,-1,sizeof(A)); memset(B,-1,sizeof(B)); dfs(0); printf("%d\n",MAX); } return 0;}
2,剪枝优化后代码:
#include <iostream>#include <algorithm>#include <cstdio>#include <queue>#include <cstring>using namespace std;int G[30][30];int MAX,n;int A[30],B[30];int P[30];void init(void){ int i,j; memset(P,0,sizeof(P)); for(j = 0;j<n;j++) P[n-1]+=G[n-1][j]; for(i = n-2;i>=0;i--){ P[i] = P[i+1]; for(j = 0;j<n;j++) P[i] += G[i][j]; }}void dfs(int cur,int SUM){ int i = 0,j = 0,k,tem; if(cur == n){ if(SUM>MAX) MAX = SUM; return ; } tem = P[cur]; if(SUM + tem < MAX) return; i = j = 0; while(A[i++]!=-1);i--; while(B[j++]!=-1);j--; A[i] = cur; for(k = 0,tem = 0;B[k]!=-1;k++) tem += G[A[i]][B[k]]; dfs(cur+1,SUM+tem); A[i] = -1; B[j] = cur; for(k = 0,tem = 0;A[k]!=-1;k++) tem += G[A[k]][B[j]]; dfs(cur+1,SUM+tem); B[j] = -1;}int main(){// freopen("data.in","r",stdin); while(scanf("%d",&n)!=EOF){ int i,j; for(i = 0;i<n;i++) for(j = 0;j<n;j++) scanf("%d",&G[i][j]); MAX = -0x3f3f3f3f; memset(A,-1,sizeof(A)); memset(B,-1,sizeof(B)); init();A[0] = 0; dfs(1,0); printf("%d\n",MAX); } return 0;}
- POJ 2531 dfs回溯(小剪枝)
- POJ 2676 Sudoku (DFS回溯剪枝)
- POJ 2907 Collecting Beepers DFS+回溯+剪枝
- POJ 1011 Sticks(DFS回溯剪枝)
- POJ 2531 (DFS+剪枝)
- POJ-2488(回溯+剪枝)
- POJ 2531 Network Saboteur(DFS+剪枝)
- POJ 2531 Network Saboteur(dfs+剪枝)
- [UVA 307] Sticks (DFS回溯 + 剪枝)
- dfs + 回溯 +剪枝
- POJ-2078(dfs + 剪枝)
- Curling 2.0 DFS+回溯+剪枝
- hdu 5113(dfs+剪枝+回溯)
- POJ 2531Network Saboteur(dfs+搜索剪枝)
- [UVA 10160] Servicing Stations (DFS回溯 + 剪枝)
- hdu1010 Tempter of the Bone(DFS,剪枝,递归,回溯)
- POJ1190 生日蛋糕 ACM解题报告(DFS回溯+剪枝)
- POJ1416 Shredding Company ACM解题报告(DFS回溯+剪枝)
- UVA 11159Factors and Multiples (最大独立集)
- JavaServer Face Study
- request.getParameter如何获取radio的属性值
- 用JQuery写拖拽的简单实例
- 用POST方式获取Radio Button是否被选中的信息
- POJ 2531 dfs回溯(小剪枝)
- ASP.NET页面通过URL传递参数(一)(转载)
- Progressbar的使用
- Asp.net页面间传值方式汇总
- NYOJ 82 迷宫寻宝(一)
- HDU 5071 Chat(大模拟)
- Android Studio 快捷键使用心得
- 字符串操作
- EasyUI-DataGrid之批量删除