题目大意:
有n个城市,有m条无向边,接下来第一行,n个数,表示每个城市现在有的士兵的数量,再接下来一行,n个数,表示每个城市需要有的士兵的数量,不能多也不能少,而且每个城市的士兵要么原地不动,要么只能移动到其直接相连的城市去(个数不限),问是否有可行方案,如果有可行方案输出出来。
思路:拆点建图。
#include <bits/stdc++.h>
using namespace std;
typedef int LL;
typedef LL lint;
const lint inf = 0x3f3f3f3f;
const int maxn = 101;
int a[maxn],n;
int res[maxn][maxn];
struct dinic{
static const int N = 10000;
static const int M = 500000;
static const lint inf = 0x3f3f3f3f;
int he[N],ne[M],ver[M],d[N];
lint edge[M];
int s,t,tot;
queue<int> que;
void init( int n ){
for( int i = 0;i <= n;i++ ){
he[i] = 0;
}
tot = 1;
}
void add( int x,int y,lint z ){
ver[++tot] = y; ne[ tot ] = he[x]; he[x] = tot; edge[ tot ] = z;
ver[++tot] = x; ne[tot] = he[y]; he[y] = tot;edge[tot] = 0;
}
bool bfs(){
memset( d,0,sizeof( d ) );
while( que.size() ) que.pop();
que.push( s );
d[s] = 1;
while( que.size() ){
int x = que.front();
que.pop();
for( int cure = he[x];cure;cure = ne[cure] ){
int y = ver[cure];
if( edge[cure] && !d[y] ){
que.push( y );
d[ y ] = d[x] + 1;
if( y == t ) return 1;
}
}
}
return 0;
}
lint dfs( int x,lint flow ){
if( x== t ) return flow;
lint rest = flow,k;
for( int cure = he[x];cure && rest;cure = ne[cure] ){
int y = ver[cure];
if( edge[cure] && d[ y ] == d[x] + 1 ){
k = dfs( y,min( rest,edge[cure] ) );
if( !k ) d[ y ] = 0;
edge[cure] -= k;
edge[ cure^1 ] += k;
rest -= k;
}
}
return flow - rest;
}
lint max_flow( int x,int y ){
s = x; t = y;
lint maxflow = 0;
lint flow = 0;
while( bfs() )
while( flow = dfs( s,inf ) ) maxflow += flow;
return maxflow;
}
void solve(){
for( int i = 1;i <= n;i++ ){
int x= i+n;
for( int cure = he[x];cure;cure = ne[cure] ){
int y = ver[cure];
if( y >=1 && y <= n ){
if(y!=i) res[i][y] = edge[ cure^1 ];
else res[i][y] = edge[cure^1];
}
}
}
}
} g;
int main(){
int m;
scanf("%d%d",&n,&m);
int x;int S = 0,T = 2*n+1;
g.init(T);int sum2 = 0;
for( int i = 1;i <= n;i++ ){
scanf("%d",&x);sum2+=x;
g.add( S,i,x );
g.add( i,i+n,x );
}
int sum = 0;
for( int i = 1;i <= n;i++ ){
scanf("%d",&x);
a[i] =x;
g.add( i,T,x );
sum += x;
}
int y;
for( int i = 1;i <= m;i++ ){
scanf("%d%d",&x,&y);
g.add( x+n,y,inf );
g.add( y+n,x,inf );
}
int ans = g.max_flow(S,T);
if( ans != sum || sum != sum2 ){
puts("NO");
return 0;
}
g.solve();
puts("YES");
for( int i = 1; i <= n;i++ ){
for( int j = 1;j <= n;j++ ){
printf("%d ",res[i][j]);
}
puts("");
}
return 0;
}