网络流(最大流) 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为节点的数量 ...
随机推荐
- Ganglia3.6.0,nginx+php搭建gweb,监控Hadoop2.2 和 Hbase0.98.1
环境:CentOS6.5 Hadoop2.2.0 Hbase0.98.1 服务端(master): 安装 ganglia ganglia-devel ganglia-gmetad ganglia-gm ...
- [FTP] FTPOperater--FTP操作帮助类 (转载)
点击下载 FTPOperater.zip 这个类是关于FTP的一些操作的1.连接FTP服务器 2.上传3.下载4.删除文件5.获取当前目录下明细(包含文件和文件夹) 6.获取FTP文件列表(包括文件 ...
- js - 多个函数动态加载
//动态添加物流锁的IEMI列表. function createLi() { var r = '<s:property value="#session.locks"/> ...
- Using native GDI for text rendering in C#
Using native GDI for text rendering in C# Aug12by Arthur To complete my previous post on text render ...
- Java:Date、Calendar、Timestamp的使用
一.Java.util.Date 该对象包含了年月日时分秒信息.具体使用如下代码: //String 转换为Date private static void dateDemo() throws Par ...
- oracle的学习 第二节:创建数据表
学习内容: A.创建数据库和表空间 B.创建用户和分配权限 C.创建数据表 一.创建数据库和表空间 (一)SQL语言的基本概念 1.概念 高级的结构化查询语言:沟通数据库服务器和客户的重要桥梁. PL ...
- (四)值栈与OGNL
所有的学习我们必须先搭建好Struts2的环境(1.导入对应的jar包,2.web.xml,3.struts.xml) 第一节:值栈简介 值栈是对应每个请求对象的一套内存数据的封装,Struts2 会 ...
- fekit前端代码模块化工具
fekit是一套前端开发工具,是由去哪儿网开发.目前在github上开源.使用fekit的优点: a.本地开发支持环境:从开发调试到上线,均是前后端工程独立开发.调试.部署,打破了原来前后端揉在一个工 ...
- ios 字符串替换方法
string=[string stringByReplacingOccurrencesOfString:@"-"withString:@"/"];
- MFC error C2065: “IDD_DIALOG1” : 未声明的标识符 转载
error C2065: “IDD_DIALOG1” : 未声明的标识符 1.编译时提示error C2065: “IDD_DIALOG1” : 未声明的标识符 2.错误的可能原因及解决方法如下: 原 ...