CF546E Soldier and Traveling

Posted five20

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CF546E Soldier and Traveling相关的知识,希望对你有一定的参考价值。

题目描述

In the country there are n n n cities and m m m bidirectional roads between them. Each city has an army. Army of the i i i -th city consists of ai a_{i} 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 b_{i} bi? soldiers in the i i i -th city.

输入输出格式

输入格式:

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

Next line contains n n n integers a1,a2,...,an a_{1},a_{2},...,a_{n} a1?,a2?,...,an? ( 0<=ai<=100 0<=a_{i}<=100 0<=ai?<=100 ).

Next line contains n n n integers b1,b2,...,bn b_{1},b_{2},...,b_{n} b1?,b2?,...,bn? ( 0<=bi<=100 0<=b_{i}<=100 0<=bi?<=100 ).

Then m m m lines follow, each of them consists of two integers p p p and q q q ( 1<=p,q<=n 1<=p,q<=n 1<=p,q<=n , p≠q p≠q pq ) denoting that there is an undirected road between cities p p p and q q 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 n n integers. Number in the i i i -th line in the j j j -th column should denote how many soldiers should road from city i i i to city j j j (if i≠j i≠j ij ) or how many soldiers should stay in city i i i (if i=j i=j 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

 

Solution:

  本题最大流,建图贼有意思。

  题意就是给定一些点上的初始士兵数,问能否通过相邻间的互相移动(只能邻边之间移动一次),达到每个点的目标士兵数。

  首先我们可以特判出一个非法情况:$sumlimits_{i=1}^{ileq n}{a_i} eqsumlimits_{i=1}^{ileq n}{b_i}$直接无解。

  然后网络流的建图比较常规,源点$s ightarrow i$边权为$a_i$,$i ightarrow j$($i==j$或者$i$与$j$相邻)边权为$inf$,$j ightarrow t$边权为$b_j$。跑出最大流后,判断总流量是否等于$a$的和,输出方案只要扫下中间连的反向边就好了。

代码:

 

 1 #include<bits/stdc++.h>
 2 #define il inline
 3 #define ll long long
 4 #define For(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
 5 #define Bor(i,a,b) for(int (i)=(b);(i)>=(a);(i)--)
 6 #define debug printf("%d %s
",__LINE__,__FUNCTION__)
 7 using namespace std;
 8 const int N=505,M=200005,inf=233333333;
 9 int n,m,s,t=250,a[N],b[N],to[M],net[M],w[M],cnt=1,h[N],dis[N];
10 int mp[N][N];
11 bool f;
12 
13 il int gi(){
14     int a=0;char x=getchar();bool f=0;
15     while((x<0||x>9)&&x!=-)x=getchar();
16     if(x==-)x=getchar(),f=1;
17     while(x>=0&&x<=9)a=(a<<3)+(a<<1)+x-48,x=getchar();
18     return f?-a:a;
19 }
20 
21 il void add(int u,int v,int c){to[++cnt]=v,net[cnt]=h[u],w[cnt]=c,h[u]=cnt;}
22 
23 il bool bfs(){
24     queue<int>q;
25     memset(dis,-1,sizeof(dis));
26     q.push(s),dis[s]=0;
27     while(!q.empty()){
28         int u=q.front();q.pop();
29         for(int i=h[u];i;i=net[i])
30             if(dis[to[i]]==-1&&w[i])dis[to[i]]=dis[u]+1,q.push(to[i]);
31     }
32     return dis[t]!=-1;
33 }
34 
35 il int dfs(int u,int op){
36     if(u==t)return op;
37     int flow=0,used=0;
38     for(int i=h[u];i;i=net[i]){
39         int v=to[i];
40         if(dis[v]==dis[u]+1&&w[i]>0){
41             used=dfs(v,min(w[i],op));
42             if(!used)continue;
43             flow+=used,op-=used;
44             w[i]-=used,w[i^1]+=used;
45             if(!op)break;
46         }
47     }
48     if(!flow)dis[u]=-1;
49     return flow;
50 }
51 
52 il void init(){
53     n=gi(),m=gi();
54     For(i,1,n) a[i]=gi(),add(s,i,a[i]),add(i,s,0); For(i,1,n) b[i]=gi(),add(i+n,t,b[i]),add(t,i+n,0);
55     int u,v,c;
56     For(i,1,m) u=gi(),v=gi(),add(u,v+n,inf),add(v+n,u,0),add(v,u+n,inf),add(u+n,v,0);
57     For(i,1,n) add(i,i+n,inf),add(i+n,i,0);
58 }
59 
60 il void solve(){
61     init();
62     int ans=0,ta=0,tb=0;
63     For(i,1,n) ta+=a[i],tb+=b[i];
64     if(ta!=tb) puts("NO");
65     else {
66         while(bfs())ans+=dfs(s,inf);
67         if(ans!=ta)puts("NO");
68         else {
69             puts("YES");
70             For(u,1,n) {
71                 for(int i=h[u+n];i;i=net[i])
72                     if(w[i]!=inf&&to[i]!=t) mp[to[i]][u]=w[i];
73             }
74             For(i,1,n) {For(j,1,n) printf("%d ",mp[i][j]); printf("
");}
75         }
76     }
77 }
78 
79 int main(){
80     solve();
81     return 0;
82 }

 

 

 

题目描述

In the country there are n n n cities and m m m bidirectional roads between them. Each city has an army. Army of the i i i -th city consists of ai a_{i} 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 b_{i} bi? soldiers in the i i i -th city.

输入输出格式

输入格式:

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

Next line contains n n n integers a1,a2,...,an a_{1},a_{2},...,a_{n} a1?,a2?,...,an? ( 0<=ai<=100 0<=a_{i}<=100 0<=ai?<=100 ).

Next line contains n n n integers b1,b2,...,bn b_{1},b_{2},...,b_{n} b1?,b2?,...,bn? ( 0<=bi<=100 0<=b_{i}<=100 0<=bi?<=100 ).

Then m m m lines follow, each of them consists of two integers p p p and q q q ( 1<=p,q<=n 1<=p,q<=n 1<=p,q<=n , p≠q p≠q pq ) denoting that there is an undirected road between cities p p p and q q 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 n n integers. Number in the i i i -th line in the j j j -th column should denote how many soldiers should road from city i i i to city j j j (if i≠j i≠j ij ) or how many soldiers should stay in city i i i (if i=j i=j 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

以上是关于CF546E Soldier and Traveling的主要内容,如果未能解决你的问题,请参考以下文章

题解 CF546C Soldier and Cards

题解CF#Bubble Cup X - Finals E-Casinos and travel

Soldier and Badges

O - Soldier and Badges

B - Soldier and Bananas

Soldier and Number Game-素数筛