51nod 1442 士兵的旅行 【 网络费用流 (还存在bug) 】
2017-02-20 15:10
357 查看
1442 士兵的旅行
题目来源: CodeForces
基准时间限制:1 秒 空间限制:131072 KB 分值: 160 难度:6级算法题
收藏
关注
在某个国家有n个城市,他们通过m条无向的道路相连。每个城市有一支军队。第i个城市的军队有ai个士兵。现在士兵开始移动。每个士兵可以呆在原地,或者走到和他所在城市直接相邻的城市,即设每一条边长度为1的话,他离开之后不能距离原来城市大于1。
判断移动之后,能不能使得第i个城市恰好有bi个士兵。
Input
Output
Input示例
Output示例
构造源点和汇点----然后直接最大流救过了--------------没有考虑只能到相邻点--------数据有点弱
代码:
题目来源: CodeForces
基准时间限制:1 秒 空间限制:131072 KB 分值: 160 难度:6级算法题
收藏
关注
在某个国家有n个城市,他们通过m条无向的道路相连。每个城市有一支军队。第i个城市的军队有ai个士兵。现在士兵开始移动。每个士兵可以呆在原地,或者走到和他所在城市直接相邻的城市,即设每一条边长度为1的话,他离开之后不能距离原来城市大于1。
判断移动之后,能不能使得第i个城市恰好有bi个士兵。
Input
单组测试数据。 第一行有两个整数n和m(1 ≤ n ≤ 100, 0 ≤ m ≤ 200)。 第二行包含n个整数 a1, a2, ..., an (0 ≤ ai ≤ 100)。 第三行包含n个整数b1, b2, ..., bn (0 ≤ bi ≤ 100)。 接下来m行,每行给出两个整数 p 和q (1 ≤ p, q ≤ n, p ≠ q),表示p和q之间有一条无向的道路。 每两个城市之间最多有一条无向的道路。
Output
如果能够调整成功,输出YES,否则输出NO。
Input示例
4 4 1 2 6 3 3 5 3 1 1 2 2 3 3 4 4 2
Output示例
YES
构造源点和汇点----然后直接最大流救过了--------------没有考虑只能到相邻点--------数据有点弱
代码:
#include<cstdio> #include<vector> #include<cstring> #include<algorithm> using namespace std; struct node{int to,cost,cap,ffv;}OP; vector<node>V[120]; int n,m,dist[110],pre1[110],pre2[110],aa[110]; void add_edge(int a,int b,int c) { OP.to=b;OP.cost=1;OP.cap=c;OP.ffv=V[b].size(); V[a].push_back(OP); OP.to=a;OP.cost=-1;OP.cap=0;OP.ffv=V[a].size()-1; V[b].push_back(OP); } bool tyuyi(int s,int t,int F) { int INF=55555; while (F) { fill(dist,dist+n+1,INF); fill(pre1,pre1+n+1,-1); fill(pre2,pre2+n+1,-1); dist[s]=0; bool update=true; while (update) { update=false; for (int i=1;i<=n;i++) { if (dist[i]==INF) continue; for (int j=0;j<V[i].size();j++) { if (V[i][j].cap>0&&dist[i]+V[i][j].cost<dist[V[i][j].to]) { update=true; dist[V[i][j].to]=dist[i]+V[i][j].cost; pre1[V[i][j].to]=i;pre2[V[i][j].to]=j; } } } } if (dist[t]==INF) return false; F--;int k=t,h; while (pre1[k]!=-1) { h=pre2[k]; k=pre1[k]; V[k][h].cap--; V[V[k][h].to][V[k][h].ffv].cap++; } } return true; } int main() { int a,b,c,s1,s2; scanf("%d%d",&n,&m); s1=s2=0; for (int i=1;i<=n;i++) { scanf("%d",&c); s1+=c; aa[i]=c; add_edge(n+1,i,c); } for (int i=1;i<=n;i++) { scanf("%d",&c); s2+=c; add_edge(i,n+2,c); } while (m--) { scanf("%d%d",&a,&b); add_edge(a,b,aa[a]); add_edge(b,a,aa[b]); } n+=2; if (s1!=s2) { printf("NO\n"); return 0; } bool fafe=tyuyi(n-1,n,s1); if (fafe) printf("YES\n"); else printf("NO\n"); return 0; }
相关文章推荐
- [51NOD] 1442 士兵的旅行 [Dinic][最大流]
- 51nod 1442 士兵的旅行(最大流)
- 51nod 1442 士兵的旅行
- 51Nod-1442-士兵的旅行
- Codeforces 546E:士兵的旅行 最大网络流
- 一个存在三年的内核 bug 引发大量的容器系统出现网络故障
- 关于“如何检测节点网络中是否存在闭环之java实现”的程序bug修复
- 一个存在三年的内核 bug 引发大量的容器系统出现网络故障
- Codeforces 546 E:士兵的旅行 最大网络流
- [Bug]:创建虚拟目录出现别名已经存在的解决方案
- 我国网络游戏策划人员存在巨大缺口
- [C#] NetworkStream.Write()存在严重bug
- 07年初更新的JavaScript引擎在处理Microsoft DHTML Behaviors时存在BUG
- CSDN博客中插入代码存在一个BUG,望大家注意下
- 最近跟踪网站,发现还是存在"常规网络错误。"
- Eclipse的新建mapping file存在BUG
- CSDN博客工具配置部分存在Bug,导致不能加载Google搜索
- 微软企业类库2.0存在的BUG
- [Bug]CSDN的Blog流量统计数据的同步存在Bug
- [DNN中的Bug]在时区编辑器中存在的一个Bug