Codeforces Round #304 (Div. 2)-E. Soldier and Traveling

解决一个关于士兵在不同城市间移动的问题,确保每个城市的士兵数量符合指定条件。通过构建网络流模型来验证是否可以达到目标状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原题链接

E. Soldier and Traveling
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 aisoldiers. 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 ≤ 1000 ≤ 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 ≤ np ≠ 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

这道题就是网络流,0为源点,和1到n城市各连一条边,边的容量为城市中士兵的数量,若两个城市i, j之间有边,则i, j+n和j, i+n各连一条边,在从n+1..2n与2n+1连边,

边的容量为各个城市最终的士兵数量


#include <iostream>
#include <cstdio> 
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
#include <set>
#include <queue>
#define maxn 100005
#define INF 1000000009
#define MOD 1000000007
typedef long long ll;
using namespace std;

struct Edge{
	Edge(){
	}
	Edge(int a, int b, int c, int d){
		from = a;
		to = b;
		flow = c;
		cap = d;
	}
	int from, to, flow, cap;
};
vector<Edge> edge;
vector<int> v[305];
int n, m, a[105], ans[105][105], vis[305], pre[305], sum, sum2;
void Add(int k1, int k2, int p){
	edge.push_back(Edge(k1, k2, 0, p));
	edge.push_back(Edge(k2, k1, 0, 0));
	int s = edge.size();
	v[k1].push_back(s-2);
	v[k2].push_back(s-1);
}
bool MaxFlow(){
	int ss = 0;
	while(1){
		queue<int> q;
		q.push(0);
		memset(vis, 0, sizeof(vis));
		vis[0] = 1e9;
		pre[0] = -1;
		while(!q.empty()){
			int h = q.front();
			q.pop();
			for(int i = 0; i < v[h].size(); i++){
				Edge e = edge[v[h][i]];
				if(vis[e.to] == 0 && e.cap > e.flow){
					vis[e.to] = min(vis[e.from], e.cap - e.flow);
					q.push(e.to);
					//printf("%d\n", e.to);
					pre[e.to] = v[h][i];
				}
			}
			if(vis[2*n+1])
			 break;
		}
		if(vis[2*n+1] == 0)
		 break;
		ss += vis[2*n+1];
		for(int j = pre[2*n+1]; j != -1; j = pre[edge[j].from]){
			edge[j].flow += vis[2*n+1];
			edge[j^1].flow -= vis[2*n+1];
		}
    }
	if(ss == sum)
	 return true;
	return false;
}
int main(){
//	freopen("in.txt", "r", stdin);
	int k1, k2;
	scanf("%d%d", &n, &m);
	for(int i = 1; i <= n; i++){
		scanf("%d", a+i);
		sum += a[i];
	}
	for(int i = 1; i <= n; i++){
		scanf("%d", &k1);
		Add(i+n, 2*n+1, k1);
		sum2 += k1;
	}
	if(sum != sum2){
		puts("NO");
		return 0;
	}
	for(int i = 0; i < m; i++){
		scanf("%d%d", &k1, &k2);
		Add(k1, k2+n, a[k1]);
		Add(k2, k1+n, a[k2]);
	}
	for(int i = 1; i <= n; i++){
	 Add(0, i, a[i]);
	 Add(i, i+n, a[i]);
    }
	if(MaxFlow()){
		puts("YES");
		for(int i = 1; i <= n; i++){
			for(int j = 0; j < v[i].size(); j++){
				int h = v[i][j];
				if(edge[h].to){
					ans[i][edge[h].to-n] += edge[h].flow;
			    }
			}
		}
		for(int i = 1; i <= n; i++){
		 for(int j = 1; j <= n; j++){
		 	if(j > 1)
		 	 putchar(' ');
		 	printf("%d", ans[i][j]);
		 }
		 puts("");
	   }
	}
	else{
		puts("NO");
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值