网络流(最大流) CodeForces 546E: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 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 soldiers in the i-th city.
Input
First line of input consists of two integers n and m (1 ≤ n ≤ 100, 0 ≤ 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 ≤ 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.
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.
Sample Input
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
水题。
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int N=,M=,INF=;
int n,m,cnt,fir[N],nxt[M],to[M],cap[M];
int dis[N],path[N],fron[N],gap[N],q[N],front,back;
struct Net_Flow{
void Init(){
memset(fir,,sizeof(fir));
memset(dis,,sizeof(dis));
memset(gap,,sizeof(gap));
front=back=cnt=;
}
void add(int a,int b,int c){
nxt[++cnt]=fir[a];
to[fir[a]=cnt]=b;
cap[cnt]=c;
}
void addedge(int a,int b,int c)
{add(a,b,c);add(b,a,);}
bool BFS(int S,int T){
q[front]=T;dis[T]=;
while(front<=back){
int x=q[front++];
for(int i=fir[x];i;i=nxt[i])
if(!dis[to[i]])dis[q[++back]=to[i]]=dis[x]+;
}
return dis[S];
}
int ISAP(int S,int T){
if(!BFS(S,T))return ;
for(int i=S;i<=T;i++)gap[dis[i]]+=;
for(int i=S;i<=T;i++)fron[i]=fir[i];
int ret=,f,p=S,Min;
while(dis[S]<=T+){
if(p==T){
f=INF;
while(p!=S){
f=min(f,cap[path[p]]);
p=to[path[p]^];
}p=T;ret+=f;
while(p!=S){
cap[path[p]]-=f;
cap[path[p]^]+=f;
p=to[path[p]^];
}
}
for(int &i=fron[p];i;i=nxt[i])
if(cap[i]&&dis[to[i]]==dis[p]-){
path[p=to[i]]=i;break;
}
if(!fron[p]){
if(!--gap[dis[p]])break;Min=T+;
for(int i=fir[p];i;i=nxt[i])
if(cap[i])Min=min(Min,dis[to[i]]);
++gap[dis[p]=Min+];fron[p]=fir[p];
if(p!=S)p=to[path[p]^];
}
}
return ret;
}
int G[N][N];
void PRINT(){
puts("YES");
for(int x=,y;x<=n;x++)
for(int i=fir[x];i;i=nxt[i])
if((y=to[i])>n&&cap[i^])
G[x][y-n]=cap[i^];
for(int x=;x<=n;x++){
for(int y=;y<=n;y++)
printf("%d ",G[x][y]);
puts("");
}
}
}isap;
int S,T;
int a[N],b[N];
int main(){
scanf("%d%d",&n,&m);
isap.Init();T=*n+;
for(int i=;i<=n;i++){
scanf("%d",&a[i]);a[]+=a[i];
isap.addedge(S,i,a[i]);
}
for(int i=;i<=n;i++){
scanf("%d",&b[i]);b[]+=b[i];
isap.addedge(i+n,T,b[i]);
isap.addedge(i,i+n,INF);
}
while(m--){int a,b;
scanf("%d%d",&a,&b);
isap.addedge(a,b+n,INF);
isap.addedge(b,a+n,INF);
}
if(a[]!=b[])puts("NO");
else if(isap.ISAP(S,T)!=a[])puts("NO");
else isap.PRINT();
return ;
}
网络流(最大流) CodeForces 546E:Soldier and Traveling的更多相关文章
- Codeforces 546E Soldier and Traveling(最大流)
题目大概说一张无向图,各个结点初始有ai人,现在每个人可以选择停留在原地或者移动到相邻的结点,问能否使各个结点的人数变为bi人. 如此建容量网络: 图上各个结点拆成两点i.i' 源点向i点连容量ai的 ...
- codeforces 546E. Soldier and Traveling 网络流
题目链接 给出n个城市, 以及初始时每个城市的人数以及目标人数.初始时有些城市是相连的. 每个城市的人只可以待在自己的城市或走到与他相邻的城市, 相邻, 相当于只能走一条路. 如果目标状态不可达, 输 ...
- 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 ...
- CF546E Soldier and Traveling(网络流,最大流)
CF546E Soldier and Traveling 题目描述 In the country there are \(n\) cities and \(m\) bidirectional road ...
- 「日常训练」 Soldier and Traveling (CFR304D2E)
题意 (CodeForces 546E) 对一个无向图,给出图的情况与各个节点的人数/目标人数.每个节点的人只可以待在自己的城市或走到与他相邻的节点. 问最后是否有解,输出一可行解(我以为是必须和答案 ...
- Soldier and Traveling
B. Soldier and Traveling Time Limit: 1000ms Memory Limit: 262144KB 64-bit integer IO format: %I64d ...
- POJ 1459-Power Network(网络流-最大流-ISAP)C++
Power Network 时间限制: 1 Sec 内存限制: 128 MB 题目描述 A power network consists of nodes (power stations, cons ...
- [POJ1273][USACO4.2]Drainage Ditches (网络流最大流)
题意 网络流最大流模板 思路 EK也不会超时 所以说是一个数据比较水的模板题 但是POJ有点坑,多组数据,而且题目没给 哭得我AC率直掉 代码 用的朴素Dinic #include<cstdio ...
- HDU 3081 Marriage Match II (网络流,最大流,二分,并查集)
HDU 3081 Marriage Match II (网络流,最大流,二分,并查集) Description Presumably, you all have known the question ...
- HDU1532 网络流最大流【EK算法】(模板题)
<题目链接> 题目大意: 一个农夫他家的农田每次下雨都会被淹,所以这个农夫就修建了排水系统,还聪明的给每个排水管道设置了最大流量:首先输入两个数n,m ;n为排水管道的数量,m为节点的数量 ...
随机推荐
- PhotoShop常用快捷键(1)
一.工具栏工具 移动工具 [V] 矩形.椭圆选框工具 [M] 套索.多边形套索.磁性套索 [L] 快速选择.魔棒工具[W] 裁剪工具 [C] 吸管.颜色取样器 [I] 修补.污点修复[J] 画笔工具 ...
- [功能帮助类] C# BaseRandom随机数,随机字符,可限制范围-帮助类 (转载)
点击下载 BaseRandom.rar 主要功能如下 .产生随机字符 .产生随机数 .在一定范围内产生随机数 看下面代码吧 /// <summary> /// 编 码 人:苏飞 /// 联 ...
- js - 在拼接字符串中动态submit当前form
今天在做一个项目的时候, mapabc中的inforWindow中,如果是超链接a,不直接响应. 后来的解决方案是动态产生form,并调用summit方法.如下 自定义一个js函数: function ...
- AutoResetEvent 详解
AutoResetEvent 允许线程通过发信号互相通信.通常,此通信涉及线程需要独占访问的资源. 线程通过调用 AutoResetEvent 上的 WaitOne 来等待信号.如果 AutoRese ...
- Java Map集合按照key和value排序之法
一.理论基点 Map是键值对的集合接口,它的实现类主要包括:HashMap,TreeMap,Hashtable以及LinkedHashMap等. TreeMap:基于红黑树(Red-Black-Tre ...
- (一)问候Struts2
第一节:Struts2 简介 主页:http://struts.apache.org/在用户请求和模块化处理方面以及页面的展现这块,Struts2 发挥的屌炸天作用:相对于传统的Jsp+Servlet ...
- C#管理异常和错误
C#管理异常和错误 1.try/catch捕捉异常的语句块,其中try{}中是写可能会出错的程序代码,catch{}中是抛出异常的代码:一个try后可以有多个catch. 2.异常采用继承层次结构进行 ...
- AngularJS cookie的使用
Javascript使用document.cookie接口来处理简单的文本cookie,但现在大多数现代浏览器都可以使用html5 API了(sessionstorage和localstorage), ...
- php验证码封装
/** * 生成验证码 * @param int $type 1: 纯数字,2:纯字母,3:数字与字母混合 * @param int $length * @return string */funct ...
- YII CRUD 例子
< ? php class PostTest extends CDbTestCase{ public $ fixtures = array ( 'posts' = > 'Post' , ' ...