CF546E 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 \(a_{i}\) 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 \(b_{i}\) soldiers in the i-th city.

输入输出格式

输入格式:

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

Next line contains \(n\) integers \(a_{1},a_{2},...,a_{n}\) ( \(0<=a_{i}<=100\) ).

Next line contains \(n\) integers \(b_{1},b_{2},...,b_{n}\)​ ( \(0<=b_{i}<=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.

输出格式:

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 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.

输入输出样例

输入样例#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


题解

题意大概就是给你 \(n\) 个城市,\(m\) 条边。

然后人只能从走相邻边相连的城市。

现在给你初始城市的每一个人数,再给一组每个城市人数。询问是否可以从当前人数变换到给定人数。如果能,输入“YES”并输出方案,不能则输出“NO”。

网络流就是建图嘛。。。

建好图一般就没有什么难度了qwq(dalao雾怼)

那么怎么建呢?

我们设一个源点和汇点。

然后把一个城市拆成两个点。

入点 \(i\) 限度设为 \(a_i\) ,出点 \(i+n\) 限度设为 \(b_{i}\) 。

这样就只要判断汇点的最大流是否与 \(b_i\) 相等了。

Ps: 原本的人数和后来方案的人数应该相等,且一定要先判定。

怎么输出方案?

看反悔路径的流量就好了。

有就标记一下并输出。


代码

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<queue>
using namespace std;
const int N=1001;
struct node{
int c,to,next;
}e[N<<1];
int num=1,head[N<<3],flag;
int dep[N<<3],n,m,a[N<<3],b[N<<3],sum,tot,s,t;
int map[N][N];
int read(){
int x=0,w=1;char ch=getchar();
while(ch>'9'||ch<'0'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
return x*w;
} void add(int from,int to,int c){
num++;
e[num].to=to;
e[num].c=c;
e[num].next=head[from];
head[from]=num;
} bool bfs(){
memset(dep,0,sizeof(dep));
queue<int>q;q.push(s);dep[s]=1;
while(!q.empty()){
int u=q.front();q.pop();
for(int i=head[u];i;i=e[i].next){
int v=e[i].to;
if(!dep[v]&&e[i].c){
dep[v]=dep[u]+1;q.push(v);
}
}
}
return dep[t];
} int dfs(int x,int cap){
if(x==t)return cap;
int addx=0;
for(int i=head[x];i;i=e[i].next){
int v=e[i].to;
if(dep[v]==dep[x]+1&&e[i].c){
int tmp=dfs(v,min(cap-addx,e[i].c));
e[i].c-=tmp;e[i^1].c+=tmp;addx+=tmp;
}
}
return addx;
} int dinic(){
int ans=0;
while(bfs())ans+=dfs(s,10000000);
return ans;
} int main()
{
n=read();m=read();s=0,t=n+n+1;
for(int i=1;i<=n;i++)a[i]=read(),add(s,i,a[i]),add(i,s,0),tot+=a[i];
for(int i=1;i<=n;i++)b[i]=read(),sum+=b[i],add(i+n,t,b[i]),add(t,i+n,0);
for(int i=1;i<=n;i++)add(i,i+n,99999999),add(i+n,i,0);
for(int i=1;i<=m;i++){
int x=read(),y=read();
// if(x<y)swap(x,y);
add(x,y+n,99999999);add(y+n,x,0);
add(y,x+n,99999999);add(x+n,y,0);
}
//cout<<sum<<' '<<dinic();
if(tot!=sum){printf("NO");return 0;}
int ans=dinic();
if(sum==ans){printf("YES\n");
for(int i=1;i<=n;i++){
for(int j=head[i];j;j=e[j].next){
int v=e[j].to;
if(v>n)
map[i][v-n]=e[j^1].c;
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++)
cout<<map[i][j]<<' ';
cout<<endl;
}
}
else printf("NO");
return 0;
}

CF546E Soldier and Traveling(网络流,最大流)的更多相关文章

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

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

  2. CF546E Soldier and Traveling

    题目描述 In the country there are n n n cities and m m m bidirectional roads between them. Each city has ...

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

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

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

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

  5. 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 ...

  6. Soldier and Traveling

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

  7. POJ 1459-Power Network(网络流-最大流-ISAP)C++

    Power Network 时间限制: 1 Sec  内存限制: 128 MB 题目描述 A power network consists of nodes (power stations, cons ...

  8. [POJ1273][USACO4.2]Drainage Ditches (网络流最大流)

    题意 网络流最大流模板 思路 EK也不会超时 所以说是一个数据比较水的模板题 但是POJ有点坑,多组数据,而且题目没给 哭得我AC率直掉 代码 用的朴素Dinic #include<cstdio ...

  9. HDU 3081 Marriage Match II (网络流,最大流,二分,并查集)

    HDU 3081 Marriage Match II (网络流,最大流,二分,并查集) Description Presumably, you all have known the question ...

随机推荐

  1. [ Docker ] 映射資料夾

    - docker run -v <host path>:<container path> - 例如:docker run -v /home/adrian/data:/data ...

  2. ZBrush功能特性之变形

    使用ZBrush内置的变形功能可以让用户对三维网格轻松应用扭曲.拉伸.弯曲及其他各种变化.在ZBrush当中,有超过20种的强大变形类型,可以应用于任何轴向.用户只需单击几次即可创造出高级形状,如图所 ...

  3. 如何使用 Open Live Writer 插入原图

    博客园的指南里写了使用 Open Live Writer 插入原图.去掉阴影并设置为默认设置的步骤,但是我还是找了好久,最后通过别的文章加上摸索才知道了如何设置为原图.这里给出详细地图片: 首先,插入 ...

  4. 在学校机房联想硬盘保护下安装Linux,并配置锐捷客户端

    最近几天一直在机房里刷题,空调开着非常舒服.但是机房电脑里全是windows系统,不太好用,挺膈应人的. 一直打算换个系统,刚才终于搞定网络问题了,以后用电脑就可以爽到了. 联想硬盘保护系统下u盘安装 ...

  5. C语言声明语句

    设计理念: C语言的一个设计理念就是声明变量和使用变量的形式应该是一致的 优点:声明变量和使用变量时的运算符优先级是相同的 缺点:运算符的优先级是C语言过度解析的部分之一 术语: 变量声明中使用到的符 ...

  6. react-native 运行提示红屏 error: bundling failed: ambiguous resolution: module `/User/xxx/Project/ico/index.js` tries to require `react-native`, but there are several files providing this module. You can de

    运行 react-native start 报错 执行这2个进行清除缓存问题 yarn start -- --reset-cache  npm start -- --reset-cache  

  7. AWS中国EC2 公网IP登录免pemKEY修改shh 配置文件

    个人使用记录 1:KEY 授权 chmod 400 VPN.pem 2:连接 ssh -i "VPN.pem" ubuntu@ec2-54-183-119-93.us-west-1 ...

  8. NYIST 1006 偷西瓜

    偷西瓜 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 对于农村的孩子来说最大的乐趣,莫过于和小伙伴们一块下地偷西瓜了,虽然孩子们条件不是很好,但是往往他们很聪明,他 ...

  9. 常用Java开源库(新手必看)

    Jakarta common: Commons LoggingJakarta Commons Logging (JCL)提供的是一个日志(Log)接口(interface),同时兼顾轻量级和不依赖于具 ...

  10. Qt之QStackedLayout

    简述 QStackedLayout继承自QLayout. QStackedLayout类提供了多页面切换的布局,一次只能看到一个界面. QStackedLayout可用于创建类似于QTabWidget ...