E. Soldier and Traveling
Time Limit: 20 Sec Memory Limit: 256 MB
题目连接
http://codeforces.com/contest/546/problem/E
Description
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.
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
4 4
1 2 6 3
3 5 3 1
1 2
2 3
3 4
4 2
Sample Output
YES
1 0 0 0
2 0 0 0
0 5 1 0
0 0 2 1
HINT
题意
给你一个图,给你一开始每个点的点权,然后问你能不能达到终态
每个点上的点权只能专递给周围的点
如果可以,请输出每个点给哪些点传了值,是多少
题解:
最大流 s-a[i]-b[i]-t这样建立图就好了
然后直接裸跑一发最大流
注意的是,要记录下每条边的flow
代码:
//qscqesze #include <cstdio> #include <cmath> #include <cstring> #include <ctime> #include <iostream> #include <algorithm> #include <set> #include <vector> #include <sstream> #include <queue> #include <typeinfo> #include <fstream> #include <map> #include <stack> typedef long long ll; using namespace std; //freopen("D.in","r",stdin); //freopen("D.out","w",stdout); #define sspeed ios_base::sync_with_stdio(0);cin.tie(0) #define maxn 5000 #define mod 10007 #define eps 1e-9 int Num; char CH[20]; //const int inf=0x7fffffff; //нчоч╢С const int inf=0x3f3f3f3f; /* inline void P(int x) { Num=0;if(!x){putchar('0');puts("");return;} while(x>0)CH[++Num]=x%10,x/=10; while(Num)putchar(CH[Num--]+48); puts(""); } */ inline ll read() { ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } inline void P(int x) { Num=0;if(!x){putchar('0');puts("");return;} while(x>0)CH[++Num]=x%10,x/=10; while(Num)putchar(CH[Num--]+48); puts(""); } //************************************************************************************** int n,m,tot,sum; int a[maxn],b[maxn]; int head[maxn],to[maxn],nxt[maxn]; int ans[maxn][maxn],flow[maxn],d[maxn],q[maxn],cap[maxn],cur[maxn]; void Add_Edge(int x,int y,int z) { to[tot]=y,nxt[tot]=head[x],cap[tot]=z,head[x]=tot++; to[tot]=x,nxt[tot]=head[y],cap[tot]=0,head[y]=tot++; } bool Bfs(int S,int T) { int f,r,x,y; f=r=0; q[++r]=T; for (int i=S;i<=T;i++) d[i]=inf; d[T]=0; while (f<r) { x=q[++f]; for (int i=head[x];i!=-1;i=nxt[i]) { y=to[i]; if (flow[i^1]==cap[i^1] || d[y]!=inf) continue; d[y]=d[x]+1; q[++r]=y; } } return d[S]!=inf; } int Dfs(int x,int T,int limit) { if (x==T) return limit; int sum=0,y,tmp; for (int &i=cur[x];i!=-1;i=nxt[i]) { y=to[i]; if (d[y]+1!=d[x] || flow[i]==cap[i]) continue; if ((tmp=Dfs(y,T,min(limit,cap[i]-flow[i])))!=0) { sum+=tmp; limit-=tmp; flow[i]+=tmp; flow[i^1]-=tmp; if (!limit) break; } } return sum; } int Maxflow(int S,int T) { int flow=0; while (Bfs(S,T)) { memcpy(cur,head,sizeof(head)); flow+=Dfs(S,T,inf); } return flow; } int main() { memset(head,-1,sizeof(head)); scanf("%d%d",&n,&m); int s=0; for (int i=1;i<=n;i++) scanf("%d",&a[i]),sum+=a[i],Add_Edge(0,i,a[i]); for (int i=1;i<=n;i++) scanf("%d",&b[i]),s+=b[i],Add_Edge(i,i+n,inf),Add_Edge(i+n,2*n+1,b[i]); if (sum!=s) { printf("NO "); return 0; } for (int i=1,x,y;i<=m;i++) { scanf("%d%d",&x,&y); Add_Edge(x,y+n,inf); Add_Edge(y,x+n,inf); } if (Maxflow(0,2*n+1)!=sum) printf("NO "); else { printf("YES "); for (int i=1;i<=n;i++) for (int j=head[i];j!=-1;j=nxt[j]) if (to[j]>n) ans[i][to[j]-n]=flow[j]; for (int i=1;i<=n;i++) { for (int j=1;j<n;j++) printf("%d ",ans[i][j]); printf("%d ",ans[i][n]); } } return 0; }