Codeforces 546 E:士兵的旅行 最大网络流
1 second
256 megabytes
standard input
standard output
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 aisoldiers.
Now soldiers roam. After roaming each soldier has to either stay in his city or to go to the one of neighboring cities by atmoving along at most one road.
Check if is it possible that after roaming there will be exactly bi soldiers
in the i-th city.
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.
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.
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
51nod上也有这道题,题意就是:
在某个国家有n个城市,他们通过m条无向的道路相连。每个城市有一支军队。第i个城市的军队有ai个士兵。现在士兵开始移动。每个士兵可以呆在原地,或者走到和他所在城市直接相邻的城市,即设每一条边长度为1的话,他离开之后不能距离原来城市大于1。
判断移动之后,能不能使得第i个城市恰好有bi个士兵。
(......)
发现题目一旦像这种有一堆点的起始状态,然后是目标状态的,并且可以用路径来更改每个点的状态的。这种题用最大网络流做准没错。
1.将源点与各个点相连,容量就是a[i]。
2.将汇点与各个点相连,容量就是b[i]。
3.将a[i]与a[i+N]相连,容量是INF,表示士兵可以留在自己的城市里面。
4.对于两个城市i与j有边的,将i与j+N相连,将j与i+N相连,容量均为INF,表示士兵可以从这两个城市来回窜。
5.求一下最大流,保证最大流填满了源点的每一条管道,填满了汇点的每一条管道。即最大流等于a[i]之和,也等于b[i]之和,就是YES。否则就是NO。
代码:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <queue>
#pragma warning(disable:4996)
using namespace std; const int sum = 205;
const int INF = 99999999; int cap[sum][sum], flow[sum][sum], a[sum], p[sum];
int val[1005];
int flag[1005];
int sum1, sum2;
int N, M; void Edmonds_Karp()
{
int u, t, result = 0;
queue <int> s;
while (s.size())s.pop(); while (1)
{
memset(a, 0, sizeof(a));
memset(p, 0, sizeof(p)); a[0] = INF;
s.push(0); while (s.size())
{
u = s.front();
s.pop(); for (t = 0; t <= M + 1; t++)
{
if (!a[t] && flow[u][t]<cap[u][t])
{
s.push(t);
p[t] = u;
a[t] = min(a[u], cap[u][t] - flow[u][t]);//要和之前的那个点,逐一比较,到M时就是整个路径的最小残量
}
}
}
if (a[M + 1] == 0)
break;
result += a[M + 1]; for (u = M + 1; u != 0; u = p[u])
{
flow[p[u]][u] += a[M + 1];
flow[u][p[u]] -= a[M + 1];
}
}
if (sum1 == result&&sum2 == result)
{
cout << "YES" << endl;
int i, j;
for (i = 1; i <= M/2; i++)
{
for (j = 1; j <= M/2; j++)
{
if (j == 1)
{
cout << flow[i][j + M/2];
}
else
{
cout <<" "<<flow[i][j + M/2];
}
}
cout << endl;
}
}
else
{
cout << "NO" << endl;
}
} int main()
{
//freopen("i.txt","r",stdin);
//freopen("o.txt","w",stdout); int i, temp, temp1, temp2;
cin >> N >> M; sum1 = 0;
sum2 = 0;
memset(cap, 0, sizeof(cap));
for (i = 1; i <= N; i++)
{
cin >> temp;
cap[0][i] = temp;
sum1 += temp;
}
for (i = 1; i <= N; i++)
{
cin >> temp;
cap[i + N][2 * N + 1] = temp;
sum2 += temp; cap[i][i + N] = INF;
}
for (i = 1; i <= M; i++)
{
cin >> temp1 >> temp2;
cap[temp1][temp2 + N] = INF;
cap[temp2][temp1 + N] = INF;
}
M = 2 * N ;
Edmonds_Karp(); //system("pause");
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
Codeforces 546 E:士兵的旅行 最大网络流的更多相关文章
- 51nod1442 士兵的旅行
裸网络流拆点就可以了... #include<cstdio> #include<cstring> #include<cctype> #include<algo ...
- CodeForces 546 D. Soldier and Number Game(素数有关)
Description Two soldiers are playing a game. At the beginning first of them chooses a positive integ ...
- Codeforces 1288F - Red-Blue Graph(上下界网络流)
Codeforces 题面传送门 & 洛谷题面传送门 好久没有写过上下界网络流了,先来一题再说( 首先先假设所有边都是蓝边,那么这样首先就有 \(b\times m\) 的花费,但是这样不一定 ...
- bzoj1570: [JSOI2008]Blue Mary的旅行(二分+网络流)
1570: [JSOI2008]Blue Mary的旅行 题目:传送门 题解: get到拆点新姿势,还是做题太少了...ORZ 因为每天就只能有一个航班,那就不能直接连了,所以要拆点(然后就被卡住了) ...
- 51nod 1442 士兵的旅行
拆点,因为只能走一步,那么u->v 后就不能到k了,这样,建图就能保证只走一步: #include <bits/stdc++.h> using namespace std; *; c ...
- 【Codeforces】【网络流】【树链剖分】【线段树】ALT (CodeForces - 786E)
题意 现在有m个人,每一个人都特别喜欢狗.另外还有一棵n个节点的树. 现在每个人都想要从树上的某个节点走到另外一个节点,且满足要么这个人自带一条狗m,要么他经过的所有边h上都有一条狗. 2<=n ...
- Codeforces 1045. A. Last chance(网络流 + 线段树优化建边)
题意 给你 \(n\) 个武器,\(m\) 个敌人,问你最多消灭多少个敌人,并输出方案. 总共有三种武器. SQL 火箭 - 能消灭给你集合中的一个敌人 \(\sum |S| \le 100000\) ...
- 【BZOJ1458】【洛谷4311】士兵占领(网络流)
[BZOJ1458][洛谷4311]士兵占领(网络流) 题面 BZOJ权限题,洛谷真好 Description 有一个M * N的棋盘,有的格子是障碍.现在你要选择一些格子来放置一些士兵,一个格子里最 ...
- Codeforces Round #546 (Div. 2) 题解
Codeforces Round #546 (Div. 2) 题目链接:https://codeforces.com/contest/1136 A. Nastya Is Reading a Book ...
随机推荐
- [运维] 如何解决 nginx: [emerg] bind() to 0.0.0.0:80 failed (13: Permission denied)
环境: 虚拟机 linux centos 7 64 当时正在配置 nginx , 由于解压后的 nginx 默认安装位置是在 /usr/local/ 目录下, 而这个目录是 root 用户才有权限操作 ...
- SpringBoot实现restuful风格的CRUD
restuful风格: 百度百科: RESTFUL是一种网络应用程序的设计风格和开发方式,基于HTTP,可以使用XML格式定义或JSON格式定义.RESTFUL适用于移动互联网厂商作为业务使能接口的场 ...
- Dart语言学习(十四) Dart泛型
什么是泛型? 通俗理解:泛型就是解决 类 接口 方法的复用性.以及对不特定数据类型的支持(类型校验) 如下代码,只能返回string类型的数据 String getData(String value) ...
- 吴裕雄--天生自然python数据清洗与数据可视化:MYSQL、MongoDB数据库连接与查询、爬取天猫连衣裙数据保存到MongoDB
本博文使用的数据库是MySQL和MongoDB数据库.安装MySQL可以参照我的这篇博文:https://www.cnblogs.com/tszr/p/12112777.html 其中操作Mysql使 ...
- [libpng]CMake+VS2015下编译libpng,及使用小例
编译前的工作 在编译libpng前,需要把zlib编译好,并加载到编译环境里. CMake + VS2015 下编译zlib,及使用小例 下载与解压 libpng的官网是 http://www.lib ...
- vscode调试开发C/C++程序
https://www.cnblogs.com/TAMING/p/8560253.html
- SpringBoot 处理异常的几种常见姿势
SpringBoot 处理异常的几种常见姿势 1. 使用 @ControllerAdvice 和 @ExceptionHandler 处理全局异常 这是目前很常用的一种方式,非常推荐.测试代码中用到了 ...
- 在 aws emr 上,将 hbase table A 的数据,对 key 做 hash,写到另外一张 table B
先 scan 原表,然后 bulkload 到新表. 采坑纪录1. bulkload 产生 hfile 前,需要先对 hash(key) 做 repartition,在 shuffle 的 read ...
- [Linux] day06——文档管理
文档管理===================mkdir 创建目录 -p /路径/目录名 (父路径不存在 -p) --------------------------------------- t ...
- “嘭、嘭、嘭”---C/S架构下的心跳机制
本人想使用AU3开发多客户端.一服务端.需要使用到心跳机制,即 在线状态实时更新以及掉线自动重连. 搜索网络发现没有人用AU3写心跳机制. 下面是一篇转帖(原文地址:http://www.cnblog ...