题目描述

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月18日)

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

  2. Git学习系列 (二)

    #克隆目录并重命名为newname,默认目录名是使用.git前的grit git clone git://github.com/schacon/grit.git newname #将所有已经跟踪过的文 ...

  3. solr 常见的问题整理 -费元星

    本文是我在开发过程中遇到的一些问题的整理,有些摘自网上别人的方法. 1. org.apache.solr.client.solrj.SolrServerException: Timeout occur ...

  4. 问题:Visual Studio 2017 无法推送到github:The requested URL returned error: 403

    问题: Visual Studio 2017 无法推送到github:The requested URL returned error: 403 原因分析: Visual Studio 2017记录的 ...

  5. 初识c++模板元编程

    模板元编程(Template metaprogramming,简称TMP)是编译器内执行的程序,编译器读入template,编译输出的结果再与其他源码一起经过普通编译过程生成目标文件.通俗来说,普通运 ...

  6. List和Turple

    List 格式:classmates = ['Michael', 'Bob', 'Tracy'] 读取list长度用:len(classmetes) 索引:索引正向从0开始,逆向从-1开始 在末尾增加 ...

  7. 【转】巫师3:狂猎(The Witcher 3: Wild Hunt )的游戏事件工作流

    转自腾讯游戏开发者平台(GAD) CDPROJEKT RED的主程序.Piotr Tomsinski 在GDC2016的最后一天18号,CDPROJEKT RED的主程Piotr Tomsinski, ...

  8. vmware中三种网络连接方式

    原文来自http://note.youdao.com/share/web/file.html?id=236896997b6ffbaa8e0d92eacd13abbf&type=note vmw ...

  9. Python中变量名里面的下划线

    1 变量名前后都有两个下划线(__X__),表示是系统级变量: 2 变量名前只有一个下划线(_X),表示该变量不是由from module import *导入进来的: 3 变量名前有两个下划线(__ ...

  10. Thunder团队第二周 - Scrum会议3

    Scrum会议3 小组名称:Thunder 项目名称:爱阅app Scrum Master:代秋彤 工作照片: 参会成员: 王航:http://www.cnblogs.com/wangh013/ 李传 ...