POJ2531 Network Saboteur dfs
来源:互联网 发布:电视盒子破解软件下载 编辑:程序博客网 时间:2024/05/16 12:52
传送门:POJ2531
题意:有几台电脑,每两台电脑之间有固定的流量,要求将这些电脑分成两部分,使这两部分之间的流量总和最大。
解法:dfs搜索其中一堆,如果将第i台电脑加进去能将两部分之间的流量变大的话,就将这一台标记出来加入这一堆,并更新流量最大值,否则就放入另一堆。
#include <iostream>#include <cstdio>#include <cstring>using namespace std;int map[22][22];int book[22];int n,ans=0;void dfs(int sum,int cnt){int t=sum;book[cnt]=1;//先将此台电脑标记出来(加入这一堆)for(int i=0;i<n;i++)//判断总流量是否变大{if(!book[i])t+=map[i][cnt];elset-=map[i][cnt];}if(t>ans)ans=t; if(t<sum)//总和没变大的话就取消标记(同时代表将此台电脑放入了第二堆) book[cnt]=0;for(int i=cnt+1;i<n;i++) { dfs(t,i);book[i]=0;//不能忘记回溯的时候取消标记}}int main(){while(~scanf("%d",&n)){ans=0;memset(book,0,sizeof(book));memset(map,0,sizeof(map));for(int i=0;i<n;i++){for(int j=0;j<n;j++)scanf("%d",&map[i][j]);}dfs(0,0);/*for(int i=0;i<n;i++)printf("%d\n",book[i]);*/printf("%d\n",ans);}return 0;}上面是我自己写的,我觉得dfs部分更好理解,但是没优化到最好。下面是百度的题解,时间缩短一些但是自己感觉理解有点困难。
#include <iostream>#include <cstdio>#include <cstring>using namespace std;int map[22][22];int book[22];int n,ans=0;void dfs(int sum,int cnt){int t=sum;book[cnt]=1;for(int i=0;i<n;i++){if(!book[i])t+=map[i][cnt];elset-=map[i][cnt];}if(t>ans)ans=t;if(t>sum)//重点就是此处不好理解,其实可以这样想,既然把这台电脑加入会使总和变小,那么一定不能在将这台电脑{<span style="white-space:pre"></span>//标记出来的情况下继续搜索,这样是毫无意义的,可以返回到上一层将标记解除后再继续搜索。for(int i=cnt+1;i<n;i++) { dfs(t,i);book[i]=0;}}}int main(){while(~scanf("%d",&n)){ans=0;memset(book,0,sizeof(book));memset(map,0,sizeof(map));for(int i=0;i<n;i++){for(int j=0;j<n;j++)scanf("%d",&map[i][j]);}dfs(0,0);/*for(int i=0;i<n;i++)printf("%d\n",book[i]);*/printf("%d\n",ans);}return 0;}
0 0
- POJ2531 Network Saboteur dfs
- poj2531(dfs)Network Saboteur
- poj2531——Network Saboteur(dfs)
- poj2531 Network Saboteur
- POJ2531--Network Saboteur
- POJ2531:Network Saboteur
- poj2531 Network Saboteur
- POJ2531.Network Saboteur
- poj2531 Network Saboteur
- POJ2531-Network Saboteur
- POJ2531 Network Saboteur DFS组合问题 || 随机算法
- POJ2531-Network Saboteur-暴力枚举+记忆化/dfs/随机化乱搞
- POJ2531——Network Saboteur
- POJ2531——Network Saboteur
- poj2531——Network Saboteur
- poj2531--Network Saboteur(搜索练习7-dfs或随机算法)
- POJ2531 Network Saboteur DFS 或 无向图的最大割
- DFS-Network Saboteur
- 第十周项目2-二叉树遍历的递归算法
- java 内存检测
- 关于Activiti explorer 中 部署时中文乱码问题
- shuf命令待完善
- C#基础知识总结(三)
- POJ2531 Network Saboteur dfs
- [HNOI2012]矿场搭建 解题报告
- Rails 根目录获取
- adb环境配置
- hadoop之MR字节码转换
- “error LNK1169: 找到一个或多个多重定义的符号”的解决方法
- 蓝桥杯-排列(例题)
- 图像拼接基础学习笔记
- jbpm异常