Codeforces Round #304 (Div. 2)E. Soldier and Traveling 网络流
2015-05-26 14:37
405 查看
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
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.
Sample test(s)
Input
Output
Input
Output
题意:n个城市,m条边, 每个点刚开始有ai个人,问最后能不能有bi个人,每个城市的人,要么不走,要么只能走到相邻的城市
思路:最大流 s-a[i]-b[i]-t建图
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
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.
Sample test(s)
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
题意:n个城市,m条边, 每个点刚开始有ai个人,问最后能不能有bi个人,每个城市的人,要么不走,要么只能走到相邻的城市
思路:最大流 s-a[i]-b[i]-t建图
#include<iostream> #include<cstdio> #include<string> #include<cstring> #include<vector> #include<cmath> #include<queue> #include<stack> #include<map> #include<set> #include<algorithm> using namespace std; const int maxn=5000; const int INF=1000000000; int N,M; int A[maxn],B[maxn]; int tot,head[maxn]; int st,en,nn,cur[maxn]; int ans[maxn][maxn]; int dis[maxn],gap[maxn],vis[maxn],pre[maxn]; struct node { int v,next,f,flow; }edge[maxn*4]; void init() { tot=0; memset(head,-1,sizeof(head)); } void add_edge(int x,int y,int f) { edge[tot].v=y; edge[tot].f=f; edge[tot].flow=0; edge[tot].next=head[x]; head[x]=tot++; edge[tot].v=x; edge[tot].f=0; edge[tot].flow=0; edge[tot].next=head[y]; head[y]=tot++; } int SAP(int st,int en) { for(int i=0;i<=nn;i++) { cur[i]=head[i]; dis[i]=gap[i]=0; } int u=0; int flow=0,aug=INF; gap[st]=nn; u=pre[st]=st; bool flag; while(dis[st]<nn) { flag=0; for(int &j=cur[u];j!=-1;j=edge[j].next) { int v=edge[j].v; if(edge[j].f>0&&dis[u]==dis[v]+1) { flag=1; if(edge[j].f<aug)aug=edge[j].f; pre[v]=u; u=v; if(u==en) { flow+=aug; while(u!=st) { u=pre[u]; edge[cur[u]].f-=aug; edge[cur[u]].flow-=aug; edge[cur[u]^1].f+=aug; edge[cur[u]^1].flow+=aug; } aug=INF; } break; } } if(flag)continue; int mindis=nn; for(int j=head[u];j!=-1;j=edge[j].next) { int v=edge[j].v; if(dis[v]<mindis&&edge[j].f>0) { mindis=dis[v]; cur[u]=j; } } if((--gap[dis[u]])==0)break; gap[dis[u]=mindis+1]++; u=pre[u]; } return flow; } int main() { scanf("%d%d",&N,&M); st=0,en=2*N+1,nn=en+1; init(); int sum=0,sum1=0; for(int i=1;i<=N;i++) { scanf("%d",&A[i]); sum+=A[i]; add_edge(st,i,A[i]); } for(int i=1;i<=N;i++) { scanf("%d",&B[i]); sum1+=B[i]; add_edge(i,i+N,INF); add_edge(i+N,en,B[i]); } while(M--) { int u,v; scanf("%d%d",&u,&v); add_edge(u,v+N,INF); add_edge(v,u+N,INF); } if(sum!=sum1){printf("NO\n");return 0;} int flow=SAP(st,en); if(flow!=sum){printf("NO\n");} else { for(int i=1;i<=N;i++) for(int j=head[i];j!=-1;j=edge[j].next) { int v=edge[j].v; ans[i][v-N]=-edge[j].flow; } printf("YES\n"); for(int i=1;i<=N;i++) { for(int j=1;j<=N;j++) printf("%d ",ans[i][j]); printf("\n"); } } return 0; }
相关文章推荐
- Codeforces Round #304 (Div. 2)E. Soldier and Traveling 网络流
- E. Soldier and Traveling(CF546E) Codeforces Round #304 (Div. 2)
- cf 304 Soldier and Traveling 网络流 + 记录流量情况
- Codeforces Round #304 (Div. 2) E. Soldier and Traveling 复习一下最大流算法
- Codeforces Round #304 (Div. 2) E - Soldier and Traveling 最大流 isap
- Codeforces Round #304 (Div. 2) E. Soldier and Traveling 最大流
- Codeforces Round #304 (Div. 2)-E. Soldier and Traveling
- Codeforces Round #304 (Div. 2) E. Soldier and Traveling 最大流
- Codeforces 546E - Soldier and Traveling (网络流输出流量)
- 网络流(最大流) CodeForces 546E:Soldier and Traveling
- Codeforces Round #304 (Div. 2) E. Soldier and Traveling
- Codeforces Round #304 (Div. 2) 546 E - Soldier and Traveling 最大流
- Codeforces Round #304 (Div. 2)-D. Soldier and Number Game,素因子打表!
- Codeforces Round #304 (Div. 2) D Soldier and Number Game
- Codeforces Round #304 (Div. 2) D - Soldier and Number Game
- Codeforces Round #304 (Div. 2) D. Soldier and Number Game 数论 筛表法
- Codeforces-B.Soldier and Badges - Codeforces Round #304 (Div. 2)
- 水题 Codeforces Round #304 (Div. 2) A. Soldier and Bananas
- Codeforces Round #304 (Div. 2) D. Soldier and Number Game
- 【codeforces 546E】Soldier and Traveling