题目描述

In the country there are n n n cities and m m m bidirectional roads between them. Each city has an army. Army of the i i i -th city consists of ai a_{i} 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 b_{i} bi​ soldiers in the i i i -th city.

输入输出格式

输入格式:

First line of input consists of two integers n n n and m m m ( 1<=n<=100 1<=n<=100 1<=n<=100 , 0<=m<=200 0<=m<=200 0<=m<=200 ).

Next line contains n n n integers a1,a2,...,an a_{1},a_{2},...,a_{n} a1​,a2​,...,an​ ( 0<=ai<=100 0<=a_{i}<=100 0<=ai​<=100 ).

Next line contains n n n integers b1,b2,...,bn b_{1},b_{2},...,b_{n} b1​,b2​,...,bn​ ( 0<=bi<=100 0<=b_{i}<=100 0<=bi​<=100 ).

Then m m m lines follow, each of them consists of two integers p p p and q q q ( 1<=p,q<=n 1<=p,q<=n 1<=p,q<=n , p≠q p≠q p≠q ) denoting that there is an undirected road between cities p p p and q q q .

It is guaranteed that there is at most one road between each pair of cities.

输出格式:

If the conditions can not be met output single word "NO".

Otherwise output word "YES" and then n n n lines, each of them consisting of n n n integers. Number in the i i i -th line in the j j j -th column should denote how many soldiers should road from city i i i to city j j j (if i≠j i≠j i≠j ) or how many soldiers should stay in city i i i (if i=j i=j i=j ).

If there are several possible answers you may output any of them.

输入输出样例

输入样例#1:

4 4
1 2 6 3
3 5 3 1
1 2
2 3
3 4
4 2
输出样例#1:

YES
1 0 0 0
2 0 0 0
0 5 1 0
0 0 2 1
输入样例#2:

2 0
1 2
2 1
输出样例#2:

NO

Solution:

  本题最大流,建图贼有意思。

  题意就是给定一些点上的初始士兵数,问能否通过相邻间的互相移动(只能邻边之间移动一次),达到每个点的目标士兵数。

  首先我们可以特判出一个非法情况:$\sum\limits_{i=1}^{i\leq n}{a_i}\neq\sum\limits_{i=1}^{i\leq n}{b_i}$直接无解。

  然后网络流的建图比较常规,源点$s\rightarrow i$边权为$a_i$,$i\rightarrow j$($i==j$或者$i$与$j$相邻)边权为$inf$,$j\rightarrow t$边权为$b_j$。跑出最大流后,判断总流量是否等于$a$的和,输出方案只要扫下中间连的反向边就好了。

代码:

#include<bits/stdc++.h>
#define il inline
#define ll long long
#define For(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
#define Bor(i,a,b) for(int (i)=(b);(i)>=(a);(i)--)
#define debug printf("%d %s\n",__LINE__,__FUNCTION__)
using namespace std;
const int N=,M=,inf=;
int n,m,s,t=,a[N],b[N],to[M],net[M],w[M],cnt=,h[N],dis[N];
int mp[N][N];
bool f; il int gi(){
int a=;char x=getchar();bool f=;
while((x<''||x>'')&&x!='-')x=getchar();
if(x=='-')x=getchar(),f=;
while(x>=''&&x<='')a=(a<<)+(a<<)+x-,x=getchar();
return f?-a:a;
} il void add(int u,int v,int c){to[++cnt]=v,net[cnt]=h[u],w[cnt]=c,h[u]=cnt;} il bool bfs(){
queue<int>q;
memset(dis,-,sizeof(dis));
q.push(s),dis[s]=;
while(!q.empty()){
int u=q.front();q.pop();
for(int i=h[u];i;i=net[i])
if(dis[to[i]]==-&&w[i])dis[to[i]]=dis[u]+,q.push(to[i]);
}
return dis[t]!=-;
} il int dfs(int u,int op){
if(u==t)return op;
int flow=,used=;
for(int i=h[u];i;i=net[i]){
int v=to[i];
if(dis[v]==dis[u]+&&w[i]>){
used=dfs(v,min(w[i],op));
if(!used)continue;
flow+=used,op-=used;
w[i]-=used,w[i^]+=used;
if(!op)break;
}
}
if(!flow)dis[u]=-;
return flow;
} il void init(){
n=gi(),m=gi();
For(i,,n) a[i]=gi(),add(s,i,a[i]),add(i,s,); For(i,,n) b[i]=gi(),add(i+n,t,b[i]),add(t,i+n,);
int u,v,c;
For(i,,m) u=gi(),v=gi(),add(u,v+n,inf),add(v+n,u,),add(v,u+n,inf),add(u+n,v,);
For(i,,n) add(i,i+n,inf),add(i+n,i,);
} il void solve(){
init();
int ans=,ta=,tb=;
For(i,,n) ta+=a[i],tb+=b[i];
if(ta!=tb) puts("NO");
else {
while(bfs())ans+=dfs(s,inf);
if(ans!=ta)puts("NO");
else {
puts("YES");
For(u,,n) {
for(int i=h[u+n];i;i=net[i])
if(w[i]!=inf&&to[i]!=t) mp[to[i]][u]=w[i];
}
For(i,,n) {For(j,,n) printf("%d ",mp[i][j]); printf("\n");}
}
}
} int main(){
solve();
return ;
}

CF546E Soldier and Traveling的更多相关文章

  1. CF546E Soldier and Traveling(网络流,最大流)

    CF546E Soldier and Traveling 题目描述 In the country there are \(n\) cities and \(m\) bidirectional road ...

  2. Codeforces Round #304 (Div. 2)(CF546E) Soldier and Traveling(最大流)

    题意 给定 n 个城市,m 条边.人只能从走相邻边相连(只能走一次)的城市. 现在给你初始城市的每一个人数,再给一组每个城市人数.询问是否可以从当前人数变换到给定人数.如果能,输入"YES& ...

  3. Codeforces Round #304 (Div. 2) E. Soldier and Traveling 最大流

    题目链接: http://codeforces.com/problemset/problem/546/E E. Soldier and Traveling time limit per test1 s ...

  4. Soldier and Traveling

    B. Soldier and Traveling Time Limit: 1000ms Memory Limit: 262144KB 64-bit integer IO format: %I64d   ...

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

    In the country there are n cities and m bidirectional roads between them. Each city has an army. Arm ...

  6. 【codeforces 546E】Soldier and Traveling

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  7. Codeforces 546E Soldier and Traveling(最大流)

    题目大概说一张无向图,各个结点初始有ai人,现在每个人可以选择停留在原地或者移动到相邻的结点,问能否使各个结点的人数变为bi人. 如此建容量网络: 图上各个结点拆成两点i.i' 源点向i点连容量ai的 ...

  8. 【CF】304 E. Soldier and Traveling

    基础网络流,增加s和t,同时对于每个结点分裂为流入结点和流出结点.EK求最大流,判断最大流是否等于当前总人数. /* 304E */ #include <iostream> #includ ...

  9. codeforces 546E. Soldier and Traveling 网络流

    题目链接 给出n个城市, 以及初始时每个城市的人数以及目标人数.初始时有些城市是相连的. 每个城市的人只可以待在自己的城市或走到与他相邻的城市, 相邻, 相当于只能走一条路. 如果目标状态不可达, 输 ...

随机推荐

  1. 佛山Uber优步司机奖励政策(12月14日到12月20日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  2. 利用Powershell查询AD中账号属性

    标签:AD账号信息 最后登录时间 最后修改密码.SID 账号SID 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://hubuxcg ...

  3. 低于0.01%的极致Crash率是怎么做到的?

    WeTest 导读 看似系统Bug的Crash 99%都不是系统问题!本文将与你一起探索Crash分析的科学方法. 在移动互联网闯荡多年的iOS手机管家,经过不断迭代创新,已经涵盖了隐私(加密相册). ...

  4. node-redis使用记录

    redis的高速存取性能让人印象深刻,虽然是分布式存储,但相比本地内存,性能毫不逊色. 之所以能做到这点,是由于redis的“单线程,多路复用IO”,同一时刻只有一个操作在进行. 而且多次建立从red ...

  5. postman使用感言

    这段时间接口测试一直使用的postman,一款谷歌接口测试插件,感受如下 优点: 1.对于中小型公司来说应该是够用的,特别是一键接口环境切换,一键设置header,作为一般的接口测试来说已经很不错了, ...

  6. Fiddler使用总结(一)

    Fiddler基础知识 .Fiddler是强大的抓包工具,它的原理是以web代理服务器的形式进行工作的,使用的代理地址是:127.0.0.1,端口默认为8888,我们也可以通过设置进行修改. .代理就 ...

  7. ant-design学习准备_1

    在学习ant-desin过程中,发现很多知识都不清楚,从现在开始,每天将自己学习到的知识进行一个总结记录,前端大佬勿扰勿喷.先介绍几个基础概念和一些常用命令: 1.什么是脚手架 我们经常在各个博客论坛 ...

  8. Linux命令应用大词典-第45章 服务器配置

    45.1 ssh-agent:存储用于公钥验证的私钥 45.2 ssh-add:添加RSA或DSA身份的认证代理 45.3 ssh-keyscan:收集主机公钥 45.4 sshd:运行sshd守护进 ...

  9. C 关键字 标示符 注释

    一 关键字 1. 什么是关键字 关键字就是C语言提供的有特殊含义的符号 也叫做"保留字" C语言一共提供了32个关键字 这些关键字都被C语言赋予了特殊含义 auto double ...

  10. Window下部署MySql数据库

    官网下载地址:https://dev.mysql.com/downloads/mysql/,MySQL Community(社区版) Server 5.7.21,下载完毕后,解压文件. (1)在mys ...