网络流(最大流) 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为节点的数量 ...
随机推荐
- CSS中Position属性
也许你看到这个标题觉得很简单,确实这是一篇关于CSS中Position属性基础知识的文章,但是关于Position的一些细节也许你不了解. 1.简介 position有五个属性: static | r ...
- .NET 统一用户管理 -- 单点登录
单点登录 前言 本篇做为.Net 统一用户管理的系列文章的开山之作,主要说一个单点登录是怎么实现的,以及为啥要统一用户管理. 单点登录(Single Sign On),简称为 SSO,是目前比较流行的 ...
- WindowsServer2003SP2EnterpriseEdition在Virtual上的安装
下载镜像(迅雷): http://192.168.0.101/WindowsServer2003SP2EnterpriseEdition.iso?fid=fWljwnwNgumTtRIy- *BY*a ...
- jvm - 内存结构以其解析
可以将jvm粗略分为以下部分: Heap Memory:存储java对象. Non-Heap Memory:存储加载的class文件,以及其他meta-data信息. Other:存储java代码,j ...
- SQL Server 索引和视图【转】
Ø 索引 1. 什么是索引 索引就是数据表中数据和相应的存储位置的列表,利用索引可以提高在表或视图中的查找数据的速度. 2. 索引分类 数据库中索引主要分为两类:聚集索引和非聚集索引.SQL Serv ...
- libctemplate——C语言模块引擎简介及使用
前言 由先声明此libctemplate不是Google那个ctemplate.这个库是用C语言实现的,只有一个实现文件和一个头文件.Gooogl的ctemplate是C++实现的,和线程还扯上了关系 ...
- php接口开发--复制缩减Codeigniter的车轮
接口需求: 输出json 单一入口 安全 http://segmentfault.com/q/1010000000143852基于token验证?session? 缓存 session cookie ...
- jQuery 获取父元素、子元素、同级元素
详情:http://www.w3school.com.cn/jquery/jquery_traversing_ancestors.asp parent() 方法返回被选元素的直接父元素.(常用) ...
- Pyqt5 实时图像滚动
实时图像 写了一个关于实时图像滚动显示的例子,做个记录. 滚动算法: 难点: 将内存数据绘制到界面,需要用到QImage和QPixmap,使用QImage转换一下,具体参见代码.这个费了好大劲才弄出来 ...
- C# 下载资源
//创建一个初始化请求对象 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri("http://wwww.b ...