【codeforces 546E】Soldier and Traveling
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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 ai 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 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.
Examples
input
4 4
1 2 6 3
3 5 3 1
1 2
2 3
3 4
4 2
output
YES
1 0 0 0
2 0 0 0
0 5 1 0
0 0 2 1
input
2 0
1 2
2 1
output
NO
【题目链接】:http://codeforces.com/problemset/problem/546/E
【题解】
题意:
每个点上的士兵只能走到相邻的点(只能走一次);
然后问你目标状态能不能达到;
做法:
按照下面这张图的规则建边;
把n个点每个点分成入点和出点两个点;
虚拟一个起点和终点(作为源点和汇点);
入点都和起点连在一起;边权为ai;
出点都和终点连在一起;边权为bi;
然后入点和出点之间接一条边;边权为INF;
如果x和y之间有一条边;
则在x的入点和y的出点之间,以及y的入点以及x的出点之间建INF的边;
然后从起点开始跑最大流;
这样造成的效果是,每个点的士兵都只会到和其距离为1的点(然后进入汇点)
显然∑ai==∑bi要满足
这样f[起点][1..i]和f[i+n][汇点]必然都是满流的;
以这个作为判断是否有解的依据;
然后输出每个点到其他点的流量就好;
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
const int MAXN = 100+10;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
int n,m,s,t;
int g[MAXN*2][MAXN*2],f[MAXN*2][MAXN*2];
int flag[MAXN*2];
int dfs(int x,int m)
{
if (x==t || !m) return m;
if (flag[x]++) return 0;
rep1(y,1,2*n+2)
{
int judge = dfs(y,min(m,g[x][y]-f[x][y]));
if (judge)
{
f[x][y]+=judge;
f[y][x]-=judge;
return judge;
}
}
return 0;
}
int main()
{
//freopen("F:\\rush.txt","r",stdin);
rei(n);rei(m);
s = 2*n+1,t = 2*n+2;
rep1(i,1,n)
{
int x;
rei(x);
g[s][i] = x;
}
rep1(i,1,n)
{
int x;
rei(x);
g[i+n][t] = x;
}
rep1(i,1,n)
g[i][i+n] = 100000;
rep1(i,1,m)
{
int x,y;
rei(x);rei(y);
g[x][y+n] = 100000;
g[y][x+n] = 100000;
}
while (dfs(s,100000))
memset(flag,0,sizeof flag);
rep1(i,1,n)
if (f[s][i]!=g[s][i] || f[i+n][t]!=g[i+n][t])
{
puts("NO");
return 0;
}
puts("YES");
rep1(i,1,n)
rep1(j,1,n)
{
printf("%d",f[i][j+n]);
if (j==n)
puts("");
else
putchar(' ');
}
return 0;
}
【codeforces 546E】Soldier and Traveling的更多相关文章
- 【codeforces 546D】Soldier and Number Game
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 546C】Soldier and Cards
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 546B】Soldier and Badges
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 546A】Soldier and Bananas
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 网络流(最大流) 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 - 546C】Soldier and Cards (vector或队列)
Soldier and Cards 老样子,直接上国语吧 Descriptions: 两个人打牌,从自己的手牌中抽出最上面的一张比较大小,大的一方可以拿对方的手牌以及自己打掉的手牌重新作为自己的牌, ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
随机推荐
- FreeNX
freenx 在Linux下,我们最常使用的远程管理工具是ssh客户端,比如putty.SecureCRT等,但是ssh只提供字符界面的操作方式,有时我们需要图形界面的操作,在Linux下支持图形界面 ...
- gerrit-申请id跟本地配置
OpenID 是一个以用户为中心的数字身份识别框架,它具有开放.分散.自由等特性. 什么是gerrit? 看 了网上的介绍,感觉所谓的gerrit就是一个基于web实现代码管理的服务器.Gerrit ...
- Android学习笔记之Bitmap位图的缩放
位图的缩放也可以借助Matrix或者Canvas来实现. 通过postScale(0.5f, 0.3f)方法设置旋转角度,然后用createBitmap方法创建一个经过缩放处理的Bitmap对象,最后 ...
- ClickOnce
Clic WPF ClickOnce应用程序IIS部署发布攻略 WPF程序非常适合公司内网使用,唯一缺点就是客户端要安装.net框架4.0.优势也很明显,在客户端运行的是一个WinForm程序,自 ...
- [置顶]
WebService学习总结(1)——WebService相关概念
一.序言 大家或多或少都听过 WebService(Web服务),有一段时间很多计算机期刊.书籍和网站都大肆的提及和宣传WebService技术,其中不乏很多吹嘘和做广告的成 分.但是不得不承认的是W ...
- Java对ad操作
转载:http://blog.csdn.net/binyao02123202/article/details/18697953
- 日志系统之基于Zookeeper的分布式协同设计
近期这段时间在设计和实现日志系统.在整个日志系统系统中Zookeeper的作用非常重要--它用于协调各个分布式组件并提供必要的配置信息和元数据.这篇文章主要分享一下Zookeeper的使用场景. 这里 ...
- css滑动鼠标到img后,切换图片
写了个样例: <a href="#"><img src="http://csdnimg.cn/pubfooter/images/gongshang_lo ...
- amazeui学习笔记--css(布局相关2)--等分网格 AVG Grid
amazeui学习笔记--css(布局相关2)--等分网格 AVG Grid 一.总结 1.与grid区别:网格中:am-g + am-u-xx-n 等分网格中只有一个: am-avg-sm-4(在u ...
- css页面滚动条出现时防止页面跳动的方法
大家写页面时应该都遇到过一个问题,尤其是写单页面应用的时候, 在有滚动条页面和没有滚动条页面之间相互跳转时, 你页面的主体内容会向左或者向右抖一下,让强迫症看了很不舒服. 现在就来解救一下强迫症: 方 ...