网络流(最大流) CodeForces 546E:Soldier and Traveling
2016-10-02 22:07
621 查看
In the country there are n cities and m bidirectional roads between them. Each city has an army. Army of the i-th city consists of ai soldiers. Now soldiers roam. After roaming each soldier has to either stay in his city or to go to the one of neighboring cities by at moving along at most one road.
Check if is it possible that after roaming there will be exactly bi soldiers in the i-th city.
Next line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 100).
Next line contains n integers b1, b2, ..., bn (0 ≤ bi ≤ 100).
Then m lines follow, each of them consists of two integers p and q (1 ≤ p, q ≤ n, p ≠ q) denoting that there is an undirected road between cities p and q.
It is guaranteed that there is at most one road between each pair of cities.
Otherwise output word "YES" and then n lines, each of them consisting of n integers. Number in the i-th line in the j-th column should denote how many soldiers should road from city i to city j (if i ≠ j) or how many soldiers should stay in city i (if i = j).
If there are several possible answers you may output any of them.
Output
Input
Output
Check if is it possible that after roaming there will be exactly bi soldiers in the i-th city.
Input
First line of input consists of two integers n and m (1 ≤ n ≤ 100, 0 ≤ m ≤ 200).Next line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 100).
Next line contains n integers b1, b2, ..., bn (0 ≤ bi ≤ 100).
Then m lines follow, each of them consists of two integers p and q (1 ≤ p, q ≤ n, p ≠ q) denoting that there is an undirected road between cities p and q.
It is guaranteed that there is at most one road between each pair of cities.
Output
If the conditions can not be met output single word "NO".Otherwise output word "YES" and then n lines, each of them consisting of n integers. Number in the i-th line in the j-th column should denote how many soldiers should road from city i to city j (if i ≠ j) or how many soldiers should stay in city i (if i = j).
If there are several possible answers you may output any of them.
Sample Input
Input4 4 1 2 6 3 3 5 3 1 1 2 2 3 3 4 4 2
Output
YES 1 0 0 0 2 0 0 0 0 5 1 0 0 0 2 1
Input
2 0 1 2 2 1
Output
NO 水题。
#include <iostream> #include <cstring> #include <cstdio> using namespace std; const int N=210,M=80010,INF=1000000000; int n,m,cnt,fir ,nxt[M],to[M],cap[M]; int dis ,path ,fron ,gap ,q ,front,back; struct Net_Flow{ void Init(){ memset(fir,0,sizeof(fir)); memset(dis,0,sizeof(dis)); memset(gap,0,sizeof(gap)); front=back=cnt=1; } void add(int a,int b,int c){ nxt[++cnt]=fir[a]; to[fir[a]=cnt]=b; cap[cnt]=c; } void addedge(int a,int b,int c) {add(a,b,c);add(b,a,0);} bool BFS(int S,int T){ q[front]=T;dis[T]=1; while(front<=back){ int x=q[front++]; for(int i=fir[x];i;i=nxt[i]) if(!dis[to[i]])dis[q[++back]=to[i]]=dis[x]+1; } return dis[S]; } int ISAP(int S,int T){ if(!BFS(S,T))return 0; for(int i=S;i<=T;i++)gap[dis[i]]+=1; for(int i=S;i<=T;i++)fron[i]=fir[i]; int ret=0,f,p=S,Min; while(dis[S]<=T+1){ if(p==T){ f=INF; while(p!=S){ f=min(f,cap[path[p]]); p=to[path[p]^1]; }p=T;ret+=f; while(p!=S){ cap[path[p]]-=f; cap[path[p]^1]+=f; p=to[path[p]^1]; } } for(int &i=fron[p];i;i=nxt[i]) if(cap[i]&&dis[to[i]]==dis[p]-1){ path[p=to[i]]=i;break; } if(!fron[p]){ if(!--gap[dis[p]])break;Min=T+1; for(int i=fir[p];i;i=nxt[i]) if(cap[i])Min=min(Min,dis[to[i]]); ++gap[dis[p]=Min+1];fron[p]=fir[p]; if(p!=S)p=to[path[p]^1]; } } return ret; } int G ; void PRINT(){ puts("YES"); for(int x=1,y;x<=n;x++) for(int i=fir[x];i;i=nxt[i]) if((y=to[i])>n&&cap[i^1]) G[x][y-n]=cap[i^1]; for(int x=1;x<=n;x++){ for(int y=1;y<=n;y++) printf("%d ",G[x][y]); puts(""); } } }isap; int S,T; int a ,b ; int main(){ scanf("%d%d",&n,&m); isap.Init();T=2*n+1; for(int i=1;i<=n;i++){ scanf("%d",&a[i]);a[0]+=a[i]; isap.addedge(S,i,a[i]); } for(int i=1;i<=n;i++){ scanf("%d",&b[i]);b[0]+=b[i]; isap.addedge(i+n,T,b[i]); isap.addedge(i,i+n,INF); } while(m--){int a,b; scanf("%d%d",&a,&b); isap.addedge(a,b+n,INF); isap.addedge(b,a+n,INF); } if(a[0]!=b[0])puts("NO"); else if(isap.ISAP(S,T)!=a[0])puts("NO"); else isap.PRINT(); return 0; }
相关文章推荐
- Codeforces 546E - Soldier and Traveling (最大流)
- CodeForces 546E - Soldier and Traveling(最大流+输出边流量)
- Codeforces 546E - Soldier and Traveling (网络流输出流量)
- 【codeforces 546E】Soldier and Traveling
- Codeforces 546 E Soldier and Traveling【最大流Dinic+判断残余网络】
- codeforces 546 E. Soldier and Traveling
- Codeforces Round #304 (Div. 2)E. Soldier and Traveling 网络流
- Codeforces Round #304 (Div. 2) E - Soldier and Traveling 最大流 isap
- [扫描线 二分图最大匹配 线段树优化网络流] Codeforces 793G Tinkoff Challenge - Elimination Round G. Oleg and chess
- Codeforces Round #304 (Div. 2) E. Soldier and Traveling 复习一下最大流算法
- 网络流(最大流)CodeForces 512C:Fox And Dinner
- Codeforces Round #304 (Div. 2)E. Soldier and Traveling 网络流
- E. Soldier and Traveling(CF546E) Codeforces Round #304 (Div. 2)
- Codeforces Round #304 (Div. 2) E. Soldier and Traveling 最大流
- Codeforces Round #304 (Div. 2) 546 E - Soldier and Traveling 最大流
- Codeforces Round #304 (Div. 2) E. Soldier and Traveling 最大流
- cf 304 Soldier and Traveling 网络流 + 记录流量情况
- CodeForces-546A-Soldier and Bananas
- CodeForces 498C Array and Operations(最大流)
- Codeforces Round #284 (Div. 1) C. Array and Operations(网络流‘最大流)