网络流(最大流) CodeForces 546E:Soldier and Traveling

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

Input
4 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=,M=,INF=;
int n,m,cnt,fir[N],nxt[M],to[M],cap[M];
int dis[N],path[N],fron[N],gap[N],q[N],front,back;
struct Net_Flow{
void Init(){
memset(fir,,sizeof(fir));
memset(dis,,sizeof(dis));
memset(gap,,sizeof(gap));
front=back=cnt=;
}
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,);}
bool BFS(int S,int T){
q[front]=T;dis[T]=;
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]+;
}
return dis[S];
}
int ISAP(int S,int T){
if(!BFS(S,T))return ;
for(int i=S;i<=T;i++)gap[dis[i]]+=;
for(int i=S;i<=T;i++)fron[i]=fir[i];
int ret=,f,p=S,Min;
while(dis[S]<=T+){
if(p==T){
f=INF;
while(p!=S){
f=min(f,cap[path[p]]);
p=to[path[p]^];
}p=T;ret+=f;
while(p!=S){
cap[path[p]]-=f;
cap[path[p]^]+=f;
p=to[path[p]^];
}
}
for(int &i=fron[p];i;i=nxt[i])
if(cap[i]&&dis[to[i]]==dis[p]-){
path[p=to[i]]=i;break;
}
if(!fron[p]){
if(!--gap[dis[p]])break;Min=T+;
for(int i=fir[p];i;i=nxt[i])
if(cap[i])Min=min(Min,dis[to[i]]);
++gap[dis[p]=Min+];fron[p]=fir[p];
if(p!=S)p=to[path[p]^];
}
}
return ret;
}
int G[N][N];
void PRINT(){
puts("YES");
for(int x=,y;x<=n;x++)
for(int i=fir[x];i;i=nxt[i])
if((y=to[i])>n&&cap[i^])
G[x][y-n]=cap[i^];
for(int x=;x<=n;x++){
for(int y=;y<=n;y++)
printf("%d ",G[x][y]);
puts("");
}
}
}isap;
int S,T;
int a[N],b[N];
int main(){
scanf("%d%d",&n,&m);
isap.Init();T=*n+;
for(int i=;i<=n;i++){
scanf("%d",&a[i]);a[]+=a[i];
isap.addedge(S,i,a[i]);
}
for(int i=;i<=n;i++){
scanf("%d",&b[i]);b[]+=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[]!=b[])puts("NO");
else if(isap.ISAP(S,T)!=a[])puts("NO");
else isap.PRINT();
return ;
}
上一篇:有关wkwebview和UIwebview获取html中的标签方法


下一篇:HDU 5025 (BFS+记忆化状压搜索)