Soldier and Traveling
B. Soldier and Traveling
64-bit integer IO format: %I64d Java class name: (Any)
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 ≤ 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
思路:最大流;
首先判断变前和变后的和是否相等,如果不等则直接输出NO,否则,转换为最大流求解,原点和原来的点连边权值为原来的人口,然后每个新的状态和汇点连边,权值为后来的人口,然后
按给的边连边,权值为原来的人口,然后跑最大流,判断最大流量是否为sum。最后的矩阵由反边得来,表示从上个点有人口转移而来,反边的值就是转移人口。
1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<stdlib.h>
5 #include<queue>
6 #include<string.h>
7 #include<map>
8 #include<vector>
9 using namespace std;
10 typedef long long LL;
11 typedef struct pp
12 {
13 int to;
14 int cap;
15 int rev;
16 } aa;
17 vector<pp>vec[205];
18 int level[205];
19 int iter[205];
20 int ans[105];
21 int bns[105];
22 void add(int from,int to,int cap);
23 void bfs(int s);
24 int dfs(int s,int t,int f);
25 int max_flow(int s,int t);
26 const int N = 1e9;
27 int ma[200][200];
28 int main(void)
29 {
30 int n,m;
31 scanf("%d %d",&n,&m);
32 int i,j;
33 int sum1 = 0;
34 int sum2 = 0;
35 for(i = 1; i <= n; i++)
36 {
37 scanf("%d",&ans[i]);
38 sum1 += ans[i];
39 }
40 for(i = 1; i <= n; i++)
41 {
42 scanf("%d",&bns[i]);
43 sum2 += bns[i];
44 }
45 if(sum1 != sum2)
46 printf("NO\n");
47 else
48 {
49 for(i = 1;i <= n; i++)
50 {
51 add(0,i,ans[i]);
52 add(i+n,2*n+1,bns[i]);
53 add(i,i+n,ans[i]);
54 }
55 while(m--)
56 {
57 int x,y;
58 scanf("%d %d",&x,&y);
59 add(x,y+n,ans[x]);
60 add(y,x+n,ans[y]);
61 }
62 int ask = max_flow(0,2*n+1);
63 if(ask != sum1)
64 printf("NO\n");
65 else
66 {
67 for(i = 1;i <= n;i++)
68 {
69 int x = i+n;
70 for(j = 0;j < vec[x].size(); j++)
71 {
72 aa no = vec[x][j];
73 ma[no.to][i] = no.cap;
74 }
75 }printf("YES\n");
76 for(i = 1;i <= n; i++)
77 {
78 for(j = 1;j <= n; j++)
79 {
80 if(j == 1)
81 printf("%d",ma[i][j]);
82 else printf(" %d",ma[i][j]);
83 }
84 printf("\n");
85 }
86 }
87 }return 0;
88 }
89 void add(int from,int to,int cap)
90 {
91 pp nn;
92 nn.to = to;
93 nn.cap = cap;
94 nn.rev = vec[to].size();
95 vec[from].push_back(nn);
96 nn.to = from;
97 nn.cap=0;
98 nn.rev = vec[from].size()-1;
99 vec[to].push_back(nn);
100 }
101 void bfs(int s)
102 {
103 queue<int>que;
104 memset(level,-1,sizeof(level));
105 level[s]=0;
106 que.push(s);
107 while(!que.empty())
108 {
109 int v=que.front();
110 que.pop();
111 int i;
112 for(i=0; i<vec[v].size(); i++)
113 {
114 pp e=vec[v][i];
115 if(level[e.to]==-1&&e.cap>0)
116 {
117 level[e.to]=level[v]+1;
118 que.push(e.to);
119 }
120 }
121 }
122 }
123 int dfs(int s,int t,int f)
124 {
125 if(s==t)
126 return f;
127 for(int &i=iter[s]; i<vec[s].size(); i++)
128 {
129 pp &e=vec[s][i];
130 if(level[e.to]>level[s]&&e.cap>0)
131 {
132 int r=dfs(e.to,t,min(e.cap,f));
133 if(r>0)
134 {
135 e.cap-=r;
136 vec[e.to][e.rev].cap+=r;
137 return r;
138 }
139 }
140 }
141 return 0;
142 }
143 int max_flow(int s,int t)
144 {
145 int flow=0;
146 for(;;)
147 {
148 bfs(s);
149 if(level[t]<0)return flow;
150 memset(iter,0,sizeof(iter));
151 int f;
152 while((f=dfs(s,t,N)) >0)
153 {
154 flow += f;
155 }
156 }
157 }
Soldier and Traveling的更多相关文章
- 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 ...
- 网络流(最大流) CodeForces 546E:Soldier and Traveling
In the country there are n cities and m bidirectional roads between them. Each city has an army. Arm ...
- CF546E Soldier and Traveling
题目描述 In the country there are n n n cities and m m m bidirectional roads between them. Each city has ...
- 【codeforces 546E】Soldier and Traveling
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- Codeforces 546E Soldier and Traveling(最大流)
题目大概说一张无向图,各个结点初始有ai人,现在每个人可以选择停留在原地或者移动到相邻的结点,问能否使各个结点的人数变为bi人. 如此建容量网络: 图上各个结点拆成两点i.i' 源点向i点连容量ai的 ...
- 【CF】304 E. Soldier and Traveling
基础网络流,增加s和t,同时对于每个结点分裂为流入结点和流出结点.EK求最大流,判断最大流是否等于当前总人数. /* 304E */ #include <iostream> #includ ...
- codeforces 546E. Soldier and Traveling 网络流
题目链接 给出n个城市, 以及初始时每个城市的人数以及目标人数.初始时有些城市是相连的. 每个城市的人只可以待在自己的城市或走到与他相邻的城市, 相邻, 相当于只能走一条路. 如果目标状态不可达, 输 ...
- 「日常训练」 Soldier and Traveling (CFR304D2E)
题意 (CodeForces 546E) 对一个无向图,给出图的情况与各个节点的人数/目标人数.每个节点的人只可以待在自己的城市或走到与他相邻的节点. 问最后是否有解,输出一可行解(我以为是必须和答案 ...
随机推荐
- 字符scanf 的输入注意
1.注意scanf 不能有空格,如果有空格会将空格给输入进去 scanf("d "):---有空格 和scanf("d");--没有空格 有很大的区别
- 什么是DDL,DML,DCL
转载自 https://www.2cto.com/database/201610/555167.html DML.DDL.DCL区别 . 总体解释: DML(data manipulation la ...
- 非标准的xml解析器的C++实现:三、解析器的初步实现
如同我之前的一篇文章说的那样,我没有支持DTD与命名空间, 当前实现出来的解析器,只能与xmlhttp对比,因为chrome浏览器解析大文档有bug,至于其他人实现的,我就不一一测试了,既然都决定自己 ...
- WINDOWS中使用svn
官网:https://tortoisesvn.net/index.zh.html (SVN安装包) 然后下载对应的64位安装包(语言包) 安装完后运行 可以存到D盘,新建一个文件夹存放 右键桌面会多 ...
- 使用Mybatis出现的问题+配置优化+ResultMap
一.可能出现的问题 1.Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: ...
- echo -e "\033[字背景颜色;字体颜色m字符串\033[0m
格式: echo -e "\033[字背景颜色;字体颜色m字符串\033[0m" 例如: echo -e "\033[41;36m something here \033 ...
- OC-copy,单例
总结 编号 主题 内容 一 NSFileManager NSFileManager介绍/用法(常见的判断)/文件访问/文件操作 二 集合对象的内存管理 集合对象的内存管理/内存管理总结 三 *copy ...
- 将图片打印到word中
1.生成模板文件 工具类: package com.sfec.snmgr.track.utils;import com.alibam.core.wechat.util.QRCodeUtil;impor ...
- Jmeter初级入门教程
<jmeter:菜鸟入门到进阶>系列 创建一个简单的自动化脚本 创建线程组[Thread Group]: 右击[TestPlan]选择[Add]--[Thread(Users)]--[Th ...
- Spring AOP通过注解的方式设置切面和切入点
切面相当于一个功能的某一个类,切入点是这个类的某部分和需要额外执行的其他代码块,这两者是多对多的关系,在代码块处指定执行的条件. Aspect1.java package com.yh.aop.sch ...