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.

Examples
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

例一的网络流的图如下,自身到自身为i->i+n
在这里插入图片描述

#include<iostream>
#include<string.h>
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<vector>
#include<cmath>
#define LL long long 
#define mem(a, b) memset(a, b, sizeof(a))
#define N 210 
#define MOD
using namespace std;
const int inf=1<<29;
int n, m, T;
int head[N], ver[2001], edge[2001], Next[2001], v[N], incf[N], pre[N];
int s, t, tot, maxflow, a[N], b[N], sum1, sum2, ans[N][N];

void add(int x, int y, int z) {
	ver[++tot]=y, edge[tot]=z, Next[tot]=head[x], head[x]=tot;
	ver[++tot]=x, edge[tot]=0, Next[tot]=head[y], head[y]=tot;
}

bool bfs() {
	mem(v, 0);
	queue<int> q;
	q.push(s); v[s]=1;
	incf[s]=inf;
	while(q.size()) {
		int x=q.front(); q.pop();
		for(int i=head[x]; i; i=Next[i])
			if(edge[i]) {
				int y=ver[i];
				if(v[y]) continue;
				incf[y]=min(incf[x], edge[i]);
				pre[y]=i;
				q.push(y), v[y]=1;
				if(y==t) return 1;
			}
	}
	return 0;
}

void update() {
	int x=t;
	while(x!=s) {
		int i=pre[x];
		edge[i]-=incf[t];
		edge[i^1]+=incf[t];
		x=ver[i^1]; 
	}
	maxflow+=incf[t];
}

int main() {
	while(scanf("%d%d",&n, &m)!=EOF) {	
		sum1=0, sum2=0;
		mem(ans, 0); 
		maxflow=0;
		mem(head, 0);
		s=0, t=2*n+1;
		tot=1;
		for(int i=1; i<=n; i++) {
			scanf("%d", &a[i]);
			sum1+=a[i];
			add(s, i, a[i]);
			add(i, i+n, inf); 
		}
		for(int i=1; i<=n; i++){
			scanf("%d",&b[i]);
			sum2+=b[i];
			add(i+n, t, b[i]);
		} 
		for(int i=1; i<=m; i++) {
			int x, y;
			scanf("%d%d", &x, &y);
			add(x, y+n, inf);
			add(y, x+n, inf); 
		}
		while(bfs()) update();
		if(sum1==sum2 && sum1==maxflow) {
			cout<<"YES\n";
			for(int i=1; i<=n; i++) {
				for(int j=head[i]; j; j=Next[j]) {
					int x=ver[j]-n;
					if(x>=1 && x<=n && edge[j^1]>0) ans[i][x]=edge[j^1];
				}
			}
			for(int i=1; i<=n; i++) {
				for(int j=1; j<=n; j++) {
					cout<<ans[i][j]<<" ";
				}
				cout<<endl;
			}
		}
		else
			cout<<"NO\n";
	}
} 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值