E. Soldier and Traveling
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

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.

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 ≤ np ≠ 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 test(s)
input
  1. 4 4
  2. 1 2 6 3
  3. 3 5 3 1
  4. 1 2
  5. 2 3
  6. 3 4
  7. 4 2
output
  1. YES
  2. 1 0 0 0
  3. 2 0 0 0
  4. 0 5 1 0
  5. 0 0 2 1
input
  1. 2 0
  2. 1 2
  3. 2 1
output
  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。

代码:

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cmath>
  4. #include <vector>
  5. #include <string>
  6. #include <cstring>
  7. #include <queue>
  8. #pragma warning(disable:4996)
  9. using namespace std;
  10.  
  11. const int sum = 205;
  12. const int INF = 99999999;
  13.  
  14. int cap[sum][sum], flow[sum][sum], a[sum], p[sum];
  15. int val[1005];
  16. int flag[1005];
  17. int sum1, sum2;
  18. int N, M;
  19.  
  20. void Edmonds_Karp()
  21. {
  22. int u, t, result = 0;
  23. queue <int> s;
  24. while (s.size())s.pop();
  25.  
  26. while (1)
  27. {
  28. memset(a, 0, sizeof(a));
  29. memset(p, 0, sizeof(p));
  30.  
  31. a[0] = INF;
  32. s.push(0);
  33.  
  34. while (s.size())
  35. {
  36. u = s.front();
  37. s.pop();
  38.  
  39. for (t = 0; t <= M + 1; t++)
  40. {
  41. if (!a[t] && flow[u][t]<cap[u][t])
  42. {
  43. s.push(t);
  44. p[t] = u;
  45. a[t] = min(a[u], cap[u][t] - flow[u][t]);//要和之前的那个点,逐一比较,到M时就是整个路径的最小残量
  46. }
  47. }
  48. }
  49. if (a[M + 1] == 0)
  50. break;
  51. result += a[M + 1];
  52.  
  53. for (u = M + 1; u != 0; u = p[u])
  54. {
  55. flow[p[u]][u] += a[M + 1];
  56. flow[u][p[u]] -= a[M + 1];
  57. }
  58. }
  59. if (sum1 == result&&sum2 == result)
  60. {
  61. cout << "YES" << endl;
  62. int i, j;
  63. for (i = 1; i <= M/2; i++)
  64. {
  65. for (j = 1; j <= M/2; j++)
  66. {
  67. if (j == 1)
  68. {
  69. cout << flow[i][j + M/2];
  70. }
  71. else
  72. {
  73. cout <<" "<<flow[i][j + M/2];
  74. }
  75. }
  76. cout << endl;
  77. }
  78. }
  79. else
  80. {
  81. cout << "NO" << endl;
  82. }
  83. }
  84.  
  85. int main()
  86. {
  87. //freopen("i.txt","r",stdin);
  88. //freopen("o.txt","w",stdout);
  89.  
  90. int i, temp, temp1, temp2;
  91. cin >> N >> M;
  92.  
  93. sum1 = 0;
  94. sum2 = 0;
  95. memset(cap, 0, sizeof(cap));
  96. for (i = 1; i <= N; i++)
  97. {
  98. cin >> temp;
  99. cap[0][i] = temp;
  100. sum1 += temp;
  101. }
  102. for (i = 1; i <= N; i++)
  103. {
  104. cin >> temp;
  105. cap[i + N][2 * N + 1] = temp;
  106. sum2 += temp;
  107.  
  108. cap[i][i + N] = INF;
  109. }
  110. for (i = 1; i <= M; i++)
  111. {
  112. cin >> temp1 >> temp2;
  113. cap[temp1][temp2 + N] = INF;
  114. cap[temp2][temp1 + N] = INF;
  115. }
  116. M = 2 * N ;
  117. Edmonds_Karp();
  118.  
  119. //system("pause");
  120. return 0;
  121. }

版权声明:本文为博主原创文章,未经博主允许不得转载。

Codeforces 546 E:士兵的旅行 最大网络流的更多相关文章

  1. 51nod1442 士兵的旅行

    裸网络流拆点就可以了... #include<cstdio> #include<cstring> #include<cctype> #include<algo ...

  2. CodeForces 546 D. Soldier and Number Game(素数有关)

    Description Two soldiers are playing a game. At the beginning first of them chooses a positive integ ...

  3. Codeforces 1288F - Red-Blue Graph(上下界网络流)

    Codeforces 题面传送门 & 洛谷题面传送门 好久没有写过上下界网络流了,先来一题再说( 首先先假设所有边都是蓝边,那么这样首先就有 \(b\times m\) 的花费,但是这样不一定 ...

  4. bzoj1570: [JSOI2008]Blue Mary的旅行(二分+网络流)

    1570: [JSOI2008]Blue Mary的旅行 题目:传送门 题解: get到拆点新姿势,还是做题太少了...ORZ 因为每天就只能有一个航班,那就不能直接连了,所以要拆点(然后就被卡住了) ...

  5. 51nod 1442 士兵的旅行

    拆点,因为只能走一步,那么u->v 后就不能到k了,这样,建图就能保证只走一步: #include <bits/stdc++.h> using namespace std; *; c ...

  6. 【Codeforces】【网络流】【树链剖分】【线段树】ALT (CodeForces - 786E)

    题意 现在有m个人,每一个人都特别喜欢狗.另外还有一棵n个节点的树. 现在每个人都想要从树上的某个节点走到另外一个节点,且满足要么这个人自带一条狗m,要么他经过的所有边h上都有一条狗. 2<=n ...

  7. Codeforces 1045. A. Last chance(网络流 + 线段树优化建边)

    题意 给你 \(n\) 个武器,\(m\) 个敌人,问你最多消灭多少个敌人,并输出方案. 总共有三种武器. SQL 火箭 - 能消灭给你集合中的一个敌人 \(\sum |S| \le 100000\) ...

  8. 【BZOJ1458】【洛谷4311】士兵占领(网络流)

    [BZOJ1458][洛谷4311]士兵占领(网络流) 题面 BZOJ权限题,洛谷真好 Description 有一个M * N的棋盘,有的格子是障碍.现在你要选择一些格子来放置一些士兵,一个格子里最 ...

  9. Codeforces Round #546 (Div. 2) 题解

    Codeforces Round #546 (Div. 2) 题目链接:https://codeforces.com/contest/1136 A. Nastya Is Reading a Book ...

随机推荐

  1. MBR扇区故障修复!

    一:进行分区且格式化硬盘 [root@roomc~]#mkfs -t ext4 /dev/sdb1    //格式化sdb1盘 二:模拟破坏/sda主硬盘破坏再修复! [root@roomc~]#mk ...

  2. 刷题62. Unique Paths

    一.题目说明 题目62. Unique Paths,在一个m*n矩阵中,求从左上角Start到右下角Finish所有路径.其中每次只能向下.向右移动.难度是Medium! 二.我的解答 这个题目读读题 ...

  3. Python爬虫连载6-cookie深入使用实例化实现自动登录

    一.使用cookie登录 1.直接把cookie复制下去,然后手动放到请求头 2.http模块包含一些关于cookie的模块,通过他们我们可以自动使用cookie (1)cookieJar 管理存储c ...

  4. Go语言学习笔记(三)

    一.浮点数 1.概述 浮点类型用于存储带有小数点的数字 一个整数数值可以赋值给浮点类型但是一个整型变量不可以赋值给浮点类型 浮点数进行运算的结果是浮点数 Go语言中浮点类型有两个 float32 fl ...

  5. 001. 使用IDEA新建一个JAVA最简单的Spring MVC JAVAWEB程序

    1. 我们打开一个空的IDEA 2. 选择Java之后点击Next 3. 点击Next创建空白工程 4. 给工程取个名字,叫MYIDEA 5. 勾选之后,点击This Window按钮 6. 我们可以 ...

  6. 剑指offer第二版速查表

    3.数组中重复数字:每个位置放置数字与下标对应相等 O(n) 4.二维数组中的查找:右下角开始比较 O(m+n) 5.替换空格:python直接替换 6.从尾到头打印链表: 借助栈或直接利用系统调用栈 ...

  7. Css——设置input输入框光标颜色

    在使用 input 输入框时,我们可能会遇到需要给其设置光标颜色的情况.谷歌浏览器的默认光标颜色是黑色的,GitHub 上的光标却是白色,那么这个用 CSS 怎么改变呢? 上面描述的情景有两种实现方式 ...

  8. 墨西哥萨卡特卡斯将举行GNOME GUADEC 2020 峰会

    导读 GNOME基金会今天宣布了下两届GUADEC(GNOME用户和开发人员欧洲会议)活动的主办城市,这也将是GNOME桌面环境下一版本的代号. 随着GNOME 3.34 “Thessalonik”的 ...

  9. Educational Codeforces Round 72 (Rated for Div. 2)D(DFS,思维)

    #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;int n,m,k=1;int c[5007] ...

  10. R语言 sample抽样函数

    Sample 函数用法: sample(x, size, replace = FALSE, prob = NULL) Arguments x - 可以是含有一个或多个元素的向量或只是一个正整数.x的长 ...