CF546E Soldier and Traveling
题目描述
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.
输入输出样例
4 4
1 2 6 3
3 5 3 1
1 2
2 3
3 4
4 2
YES
1 0 0 0
2 0 0 0
0 5 1 0
0 0 2 1
2 0
1 2
2 1
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的更多相关文章
- CF546E Soldier and Traveling(网络流,最大流)
CF546E Soldier and Traveling 题目描述 In the country there are \(n\) cities and \(m\) bidirectional road ...
- Codeforces Round #304 (Div. 2)(CF546E) Soldier and Traveling(最大流)
题意 给定 n 个城市,m 条边.人只能从走相邻边相连(只能走一次)的城市. 现在给你初始城市的每一个人数,再给一组每个城市人数.询问是否可以从当前人数变换到给定人数.如果能,输入"YES& ...
- 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 ...
- Soldier and Traveling
B. Soldier and Traveling Time Limit: 1000ms Memory Limit: 262144KB 64-bit integer IO format: %I64d ...
- 网络流(最大流) CodeForces 546E:Soldier and Traveling
In the country there are n cities and m bidirectional roads between them. Each city has an army. Arm ...
- 【codeforces 546E】Soldier and Traveling
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- Codeforces 546E Soldier and Traveling(最大流)
题目大概说一张无向图,各个结点初始有ai人,现在每个人可以选择停留在原地或者移动到相邻的结点,问能否使各个结点的人数变为bi人. 如此建容量网络: 图上各个结点拆成两点i.i' 源点向i点连容量ai的 ...
- 【CF】304 E. Soldier and Traveling
基础网络流,增加s和t,同时对于每个结点分裂为流入结点和流出结点.EK求最大流,判断最大流是否等于当前总人数. /* 304E */ #include <iostream> #includ ...
- codeforces 546E. Soldier and Traveling 网络流
题目链接 给出n个城市, 以及初始时每个城市的人数以及目标人数.初始时有些城市是相连的. 每个城市的人只可以待在自己的城市或走到与他相邻的城市, 相邻, 相当于只能走一条路. 如果目标状态不可达, 输 ...
随机推荐
- Linux 下获取本机IP
http://blog.csdn.net/K346K346/article/details/48231933 int main () { /* struct ifaddrs *ifap, *ifa; ...
- postman使用感言
这段时间接口测试一直使用的postman,一款谷歌接口测试插件,感受如下 优点: 1.对于中小型公司来说应该是够用的,特别是一键接口环境切换,一键设置header,作为一般的接口测试来说已经很不错了, ...
- android自动化のadb常用命令(不定期更新)
1. adb devices 执行结果是adb为每一个设备输出以下状态信息:序列号(serialNumber) — 由adb创建的使用控制台端口号的用于唯一标识一个模拟器或手机设备的字符串,格式是 & ...
- Python-S9——Day82-CRM项目实战
1.权限的概念: 2.RBAC的设计: 3.注册登录用户所有权限到session中: 4.权限的校验: 5.基于中间件的权限校验: 1.权限的概念: 1.1 项目与应用: Project App 1. ...
- (原) MaterialEditor部- UmateriaEditor中 Node编译过程和使用(2)
@白袍小道 转载说明原处 插件同步在GITHUB: DaoZhang_XDZ 需求: 1.梳理FexpressionInput和Output的编译和链接(套路和逻辑目的) 2.如何做到节点编译 ...
- NOIP2019普及级别模拟 3.30校模拟
好吧我还是第一次写这种总结类的玩意… 考场心情…hmm…我没睡醒.是的是这样的,反正题都有两三个看错了或者没看懂… 最关键的是!!我!居!然!把!Freopen!写!在!了!程!序!最!后! 然后就和 ...
- 基于Hadoop2.5.0的集群搭建
http://download.csdn.net/download/yameing/8011891 一. 规划 1. 准备安装包 JDK:http://download.oracle.com/otn ...
- Special Offer! Super Price 999 Bourles!
Description Polycarpus is an amateur businessman. Recently he was surprised to find out that the mar ...
- http-bio-8080"-exec-6"(转)
现象如下: Tomcat7启动后,后台抛出如下异常,前台一直无法登陆Exception in thread ""http-bio-8080"-exec-6" j ...
- Java 类和Static关键字
类的定义 类的命名.首字母大写 大括号后面没有分号 成员变量 Java会自动初始化成员变量但是不会自动初始化局部变量: 可以在定义成员变量是直接初始化,成员变量的作用范围在整个类体 对象的创建和引用的 ...