codeforces 546E. Soldier and Traveling 网络流
给出n个城市, 以及初始时每个城市的人数以及目标人数。初始时有些城市是相连的。 每个城市的人只可以待在自己的城市或走到与他相邻的城市, 相邻, 相当于只能走一条路。
如果目标状态不可达, 输出no, 否则输出每个城市的人都是怎么走的, 比如第一个城市有2个人走到了第二个城市, 1个人留在了第一个城市, 那么输出的第一行前两个数就是1, 2。
很明显的网络流, 输出那里写了好久...
首先判断能否可达, 如果初始状态的人数与目标状态不一样, 一定不可达, 其次, 如果建完图跑网络流的结果与所有目标城市人数的和不一样, 也不可达。
建图的话, 将一个城市拆成两个点, (u, u') 中间连边inf, 说明可以随便走, 源点和u相连, 权值为初始状态人数, u'与汇点相连, 人数为目标状态人数。 如果两个城市之间有路相连, 那么加边(u, v'), (v, u'), 权值为inf。
每个城市的人是怎么走的, 应该看反向边的流量, 如果边(v', u)的权值为x, 那么ans[u][v] = x。
具体看代码。
#include<bits/stdc++.h>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, a, n) for(int i = a; i<n; i++)
#define ull unsigned long long
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
const int maxn = 2e5;
int q[maxn*], head[maxn*], dis[maxn/], s, t, num, vis[], val[][];
struct node
{
int to, nextt, c;
node(){}
node(int to, int nextt, int c):to(to), nextt(nextt), c(c){}
}e[maxn*];
void init() {
num = ;
mem1(head);
}
void add(int u, int v, int c) {
e[num] = node(v, head[u], c); head[u] = num++;
e[num] = node(u, head[v], ); head[v] = num++;
}
int bfs() {
mem(dis);
dis[s] = ;
int st = , ed = ;
q[ed++] = s;
while(st<ed) {
int u = q[st++];
for(int i = head[u]; ~i; i = e[i].nextt) {
int v = e[i].to;
if(!dis[v]&&e[i].c) {
dis[v] = dis[u]+;
if(v == t)
return ;
q[ed++] = v;
}
}
}
return ;
}
int dfs(int u, int limit) {
if(u == t) {
return limit;
}
int cost = ;
for(int i = head[u]; ~i; i = e[i].nextt) {
int v = e[i].to;
if(e[i].c&&dis[v] == dis[u]+) {
int tmp = dfs(v, min(limit-cost, e[i].c));
if(tmp>) {
e[i].c -= tmp;
e[i^].c += tmp;
cost += tmp;
if(cost == limit)
break;
} else {
dis[v] = -;
}
}
}
return cost;
}
int dinic() {
int ans = ;
while(bfs()) {
ans += dfs(s, inf);
}
return ans;
}
int main()
{
int n, m, x, y, sum = , tmp = ;
cin>>n>>m;
init();
s = , t = n*+;
for(int i = ; i<=n; i++) {
scanf("%d", &x);
add(s, i, x);
tmp += x;
}
for(int i = ; i<=n; i++) {
scanf("%d", &x);
add(i+n, t, x);
sum += x;
add(i, i+n, inf);
}
while(m--) {
scanf("%d%d", &x, &y);
add(x, y+n, inf);
add(y, x+n, inf);
}
int ans = dinic();
if(ans != sum||sum!=tmp) {
cout<<"NO"<<endl;
return ;
}
cout<<"YES"<<endl;
for(int u = ; u<=n; u++) {
for(int i = head[u]; ~i; i = e[i].nextt) {
int v = e[i].to;
if(v>n) {
val[u][v-n] = e[i^].c; //反向边流量
}
}
}
for(int i = ; i<=n; i++) {
for(int j = ; j<=n; j++) {
cout<<val[i][j]<<" ";
}
cout<<endl;
}
return ;
}
codeforces 546E. Soldier and Traveling 网络流的更多相关文章
- Codeforces 546E Soldier and Traveling(最大流)
题目大概说一张无向图,各个结点初始有ai人,现在每个人可以选择停留在原地或者移动到相邻的结点,问能否使各个结点的人数变为bi人. 如此建容量网络: 图上各个结点拆成两点i.i' 源点向i点连容量ai的 ...
- 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 ...
- 网络流(最大流) 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 Round #304 (Div. 2)(CF546E) Soldier and Traveling(最大流)
题意 给定 n 个城市,m 条边.人只能从走相邻边相连(只能走一次)的城市. 现在给你初始城市的每一个人数,再给一组每个城市人数.询问是否可以从当前人数变换到给定人数.如果能,输入"YES& ...
- Codeforces 1045. A. Last chance(网络流 + 线段树优化建边)
题意 给你 \(n\) 个武器,\(m\) 个敌人,问你最多消灭多少个敌人,并输出方案. 总共有三种武器. SQL 火箭 - 能消灭给你集合中的一个敌人 \(\sum |S| \le 100000\) ...
随机推荐
- 解决windows下Eclipse连接远程Hadoop报错
Exception in thread "main" java.lang.UnsatisfiedLinkError: org.apache.hadoop.io.nativeio.N ...
- boost signal2 trackable
挺简单的一个类,只是维护了一个成员 shared_ptr<detail::trackable_pointee> _tracked_ptr; 这样看来的话,所谓的track还是基于智能指针, ...
- 关于require,require_once,include和include_once的区别
一.定义 require,require_once,include,include_once都属于PHP的关键字,也就是说它们实际都是PHP的语句,而不是函数,类似于print,echo一样,也是PH ...
- c语言中的制表符\t与空格
(本文不讨论制表符与空格缩进问题) 编程过程中,我们常常用多个空格或制表符分隔两个字符串,那么这两个在显示效果上有什么区别呢? 比较如下两行代码的输出效果 代码1: printf("1\t1 ...
- 百度地图Label 样式:label.setStyle
创建文本标注对象设置样式的时候,其中的backgroundColor属性居然还支持透明啊,不过改变数值好像对效果没有影响 var numLabel = new BMap.Label(num); num ...
- 关于“#ifdef __cplusplus”
CC++语言在编译的时候为了解决函数的多态问题,会将函数名和参数联合起来生成一个中间的函数名称,而C语言则不会,因此会造成链接时找不到对应函数的情况,此时C函数就需要用extern “C”进行链接指定 ...
- 原来ipad的浏览器也可以直接clip到evernote
今天才发现是有方法通过邮件方式保存ipad上浏览的内容到evernote,之前以为要反复切换app来做到. 只要在toread.cc登记evernote对应帐号的邮箱,就可以根据toread返回到ev ...
- 字符串比较必须使用strcmp
char s1[]="this" char *s2 = "this" if(s1=="this"){ printf("s1 is ...
- VC++2008 用空工程创建 DLL
VC++2008 用空工程创建 DLL 一.创建 DLL 工程项目: 1)点击菜单[File] -> [New] -> [Project...] 弹出 “New Project” 对话框: ...
- Android 模拟系统事件(三)
简介 Android系统是基于Linux内核的,而Linux内核继承和兼容了丰富的Unix系统进程间通信(IPC)机制.Binder其实也不是Android提出来的一套新的进程间通信机制,它是基于Op ...