CF546E 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 \(a_{i}\) 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 \(b_{i}\) soldiers in the i-th city.

输入输出格式

输入格式:

First line of input consists of two integers n n n and m m m ( \(1<=n<=100\) , \(0<=m<=200\) ).

Next line contains \(n\) integers \(a_{1},a_{2},...,a_{n}\) ( \(0<=a_{i}<=100\) ).

Next line contains \(n\) integers \(b_{1},b_{2},...,b_{n}\)​ ( \(0<=b_{i}<=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 n 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.

输入输出样例

输入样例#1: 复制

4 4

1 2 6 3

3 5 3 1

1 2

2 3

3 4

4 2

输出样例#1: 复制

YES

1 0 0 0

2 0 0 0

0 5 1 0

0 0 2 1

输入样例#2: 复制

2 0

1 2

2 1

输出样例#2: 复制

NO


题解

题意大概就是给你 \(n\) 个城市,\(m\) 条边。

然后人只能从走相邻边相连的城市。

现在给你初始城市的每一个人数,再给一组每个城市人数。询问是否可以从当前人数变换到给定人数。如果能,输入“YES”并输出方案,不能则输出“NO”。

网络流就是建图嘛。。。

建好图一般就没有什么难度了qwq(dalao雾怼)

那么怎么建呢?

我们设一个源点和汇点。

然后把一个城市拆成两个点。

入点 \(i\) 限度设为 \(a_i\) ,出点 \(i+n\) 限度设为 \(b_{i}\) 。

这样就只要判断汇点的最大流是否与 \(b_i\) 相等了。

Ps: 原本的人数和后来方案的人数应该相等,且一定要先判定。

怎么输出方案?

看反悔路径的流量就好了。

有就标记一下并输出。


代码

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<cmath>
  4. #include<algorithm>
  5. #include<iostream>
  6. #include<queue>
  7. using namespace std;
  8. const int N=1001;
  9. struct node{
  10. int c,to,next;
  11. }e[N<<1];
  12. int num=1,head[N<<3],flag;
  13. int dep[N<<3],n,m,a[N<<3],b[N<<3],sum,tot,s,t;
  14. int map[N][N];
  15. int read(){
  16. int x=0,w=1;char ch=getchar();
  17. while(ch>'9'||ch<'0'){if(ch=='-')w=-1;ch=getchar();}
  18. while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
  19. return x*w;
  20. }
  21. void add(int from,int to,int c){
  22. num++;
  23. e[num].to=to;
  24. e[num].c=c;
  25. e[num].next=head[from];
  26. head[from]=num;
  27. }
  28. bool bfs(){
  29. memset(dep,0,sizeof(dep));
  30. queue<int>q;q.push(s);dep[s]=1;
  31. while(!q.empty()){
  32. int u=q.front();q.pop();
  33. for(int i=head[u];i;i=e[i].next){
  34. int v=e[i].to;
  35. if(!dep[v]&&e[i].c){
  36. dep[v]=dep[u]+1;q.push(v);
  37. }
  38. }
  39. }
  40. return dep[t];
  41. }
  42. int dfs(int x,int cap){
  43. if(x==t)return cap;
  44. int addx=0;
  45. for(int i=head[x];i;i=e[i].next){
  46. int v=e[i].to;
  47. if(dep[v]==dep[x]+1&&e[i].c){
  48. int tmp=dfs(v,min(cap-addx,e[i].c));
  49. e[i].c-=tmp;e[i^1].c+=tmp;addx+=tmp;
  50. }
  51. }
  52. return addx;
  53. }
  54. int dinic(){
  55. int ans=0;
  56. while(bfs())ans+=dfs(s,10000000);
  57. return ans;
  58. }
  59. int main()
  60. {
  61. n=read();m=read();s=0,t=n+n+1;
  62. for(int i=1;i<=n;i++)a[i]=read(),add(s,i,a[i]),add(i,s,0),tot+=a[i];
  63. for(int i=1;i<=n;i++)b[i]=read(),sum+=b[i],add(i+n,t,b[i]),add(t,i+n,0);
  64. for(int i=1;i<=n;i++)add(i,i+n,99999999),add(i+n,i,0);
  65. for(int i=1;i<=m;i++){
  66. int x=read(),y=read();
  67. // if(x<y)swap(x,y);
  68. add(x,y+n,99999999);add(y+n,x,0);
  69. add(y,x+n,99999999);add(x+n,y,0);
  70. }
  71. //cout<<sum<<' '<<dinic();
  72. if(tot!=sum){printf("NO");return 0;}
  73. int ans=dinic();
  74. if(sum==ans){printf("YES\n");
  75. for(int i=1;i<=n;i++){
  76. for(int j=head[i];j;j=e[j].next){
  77. int v=e[j].to;
  78. if(v>n)
  79. map[i][v-n]=e[j^1].c;
  80. }
  81. }
  82. for(int i=1;i<=n;i++){
  83. for(int j=1;j<=n;j++)
  84. cout<<map[i][j]<<' ';
  85. cout<<endl;
  86. }
  87. }
  88. else printf("NO");
  89. return 0;
  90. }

CF546E Soldier and Traveling(网络流,最大流)的更多相关文章

  1. Codeforces Round #304 (Div. 2)(CF546E) Soldier and Traveling(最大流)

    题意 给定 n 个城市,m 条边.人只能从走相邻边相连(只能走一次)的城市. 现在给你初始城市的每一个人数,再给一组每个城市人数.询问是否可以从当前人数变换到给定人数.如果能,输入"YES& ...

  2. CF546E Soldier and Traveling

    题目描述 In the country there are n n n cities and m m m bidirectional roads between them. Each city has ...

  3. Codeforces 546E Soldier and Traveling(最大流)

    题目大概说一张无向图,各个结点初始有ai人,现在每个人可以选择停留在原地或者移动到相邻的结点,问能否使各个结点的人数变为bi人. 如此建容量网络: 图上各个结点拆成两点i.i' 源点向i点连容量ai的 ...

  4. codeforces 546E. Soldier and Traveling 网络流

    题目链接 给出n个城市, 以及初始时每个城市的人数以及目标人数.初始时有些城市是相连的. 每个城市的人只可以待在自己的城市或走到与他相邻的城市, 相邻, 相当于只能走一条路. 如果目标状态不可达, 输 ...

  5. 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 ...

  6. Soldier and Traveling

    B. Soldier and Traveling Time Limit: 1000ms Memory Limit: 262144KB 64-bit integer IO format: %I64d   ...

  7. POJ 1459-Power Network(网络流-最大流-ISAP)C++

    Power Network 时间限制: 1 Sec  内存限制: 128 MB 题目描述 A power network consists of nodes (power stations, cons ...

  8. [POJ1273][USACO4.2]Drainage Ditches (网络流最大流)

    题意 网络流最大流模板 思路 EK也不会超时 所以说是一个数据比较水的模板题 但是POJ有点坑,多组数据,而且题目没给 哭得我AC率直掉 代码 用的朴素Dinic #include<cstdio ...

  9. HDU 3081 Marriage Match II (网络流,最大流,二分,并查集)

    HDU 3081 Marriage Match II (网络流,最大流,二分,并查集) Description Presumably, you all have known the question ...

随机推荐

  1. JavaScript语法高亮库highlight.js使用

    highlight.js是一款基于JavaScript的语法高亮库,目前支持125种编程语言,有63种可供选择的样式,而且能够做到语言自动识别,和目前主流的JS框架都能兼容,可以混合使用. 这款高亮库 ...

  2. LeetCode 1. Two Sum (c++ stl map)

    题目:https://leetcode.com/problems/two-sum/description/ stl map代码: class Solution { public: vector< ...

  3. C# 运算符 ?、??、?: 、?. 、 各种问号的用法和说明

    1. 可空类型修饰符(?):引用类型可以使用空引用表示一个不存在的值,而值类型通常不能表示为空.例如:string str=null; 是正确的,int i=null; 编译器就会报错.为了使值类型也 ...

  4. 3DES(或称为Triple DES)是三重数据加密算法(TDEA,Triple Data Encryption Algorithm)

    3DES(或称为Triple DES)是三重数据加密算法(TDEA,Triple Data Encryption Algorithm)块密码的通称.它相当于是对每个数据块应用三次DES加密算法.由于计 ...

  5. js或者jq 使用cookie 时在谷歌浏览器不好使

    用js或者jq 写cookie时在谷歌浏览器上打开,cookie不能正常使用. 原因:浏览器没有开启cookie,打开cookie 就可以显示 其次,当将代码上传至服务器,再用浏览器打开时,cooki ...

  6. swift语言点评十六-Initialization && Deinitialization

    initial value:必须初始化.不影响观察者 Classes and structures must set all of their stored properties to an appr ...

  7. 解决IIS服务器部署 字体图标找不到的原因

    引言 我们往往在IIS上部署Web项目,或者发布Web项目的时候,经常会遇到浏览器找不到字体文件(woff/woff2)产生的错误.这样会导致浏览器无法加载字体图标,在影响加载时间的同时,更无法显示对 ...

  8. php对excel导入导出的支持

    闲话不多说了,大家直接进入主题 php对excel的导入:  1.为程序添加一个form表单,为form标签添加“enctype="multipart/form-data"”属性 ...

  9. javascript中实现继承的几种方式

    javascript中实现继承的几种方式 1.借用构造函数实现继承 function Parent1(){ this.name = "parent1" } function Chi ...

  10. 一句话木马和中国菜刀的结合拿webshell

    什么叫做一句话木马:     就是一句简单的脚本语言,一句话木马分为Php,asp,aspx等 中国菜刀:   连接一句话木马的工具 实验的目的:  通过一句话木马来控制我们的服务器,拿到webshe ...