Codeforces 546E Soldier and Traveling (最大流)

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 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 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 ≤ 1000 ≤ 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 ≤ np ≠ 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条边的无向图,每个点上有若干名士兵,每名士兵只能留在原地或移动到相邻点上,给出移动后n个顶点的士兵人数,问是否存在一种移动方案.


分析:

建图,对i=1,2,...,n,源点S向标号i的点连容量为a[i]的边,标号i+n的点向汇点T连容量为b[i]的边,标号i的点向标号i+n的点连容量为INF的边(表示留在原地),若原图中i,j两点间有边相连,则标号i的点向标号j+n的点连容量为INF的边(表示可以从i到j),标号j的点向标号i+n的点连容量为INF的边(表示可以从j到i),跑一次从S到T的最大流,如果最大流既与所有a[i]之和相等,又与所有b[i]之和相等,则有解。


代码:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN=1005;
const int MAXM=3005;
const int INF=0x3f3f3f3f;
struct Edge
{
    int to,next,cap,flow;
}edge[MAXM];
int tol,head[MAXN];
int gap[MAXN],dep[MAXN],pre[MAXN],cur[MAXN];
void init()
{
    tol=0;
    memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int w,int rw=0)
{
    edge[tol].to=v;
    edge[tol].cap=w;
    edge[tol].next=head[u];
    edge[tol].flow=0;
    head[u]=tol++;
    edge[tol].to=u;
    edge[tol].cap=rw;
    edge[tol].next=head[v];
    edge[tol].flow=0;
    head[v]=tol++;
}
int sap(int st,int ed,int N)
{
    memset(gap,0,sizeof(gap));
    memset(dep,0,sizeof(dep));
    memcpy(cur,head,sizeof(head));
    int u=st;
    pre[u]=-1;
    gap[0]=N;
    int ans=0;
    while(dep[st]<N)
    {
        if(u==ed)
        {
            int Min=INF;
            for(int i=pre[u];i!=-1;i=pre[edge[i^1].to])
                if(Min>edge[i].cap-edge[i].flow)
                    Min=edge[i].cap-edge[i].flow;
            for(int i=pre[u];i!=-1;i=pre[edge[i^1].to])
            {
                edge[i].flow+=Min;
                edge[i^1].flow-=Min;
            }
            u=st;
            ans+=Min;
            continue;
        }
        bool flag=0;
        int v;
        for(int i=cur[u];i!=-1;i=edge[i].next)
        {
            v=edge[i].to;
            if(edge[i].cap-edge[i].flow && dep[v]+1==dep[u])
            {
                flag=1;
                cur[u]=pre[v]=i;
                break;
            }
        }
        if(flag)
        {
            u=v;
            continue;
        }
        int Min=N;
        for(int i=head[u];i!=-1;i=edge[i].next)
            if(edge[i].cap-edge[i].flow && dep[edge[i].to]<Min)
            {
                Min=dep[edge[i].to];
                cur[u]=i;
            }
        gap[dep[u]]--;
        if(!gap[dep[u]])return ans;
        dep[u]=Min+1;
        gap[dep[u]]++;
        if(u!=st)u=edge[pre[u]^1].to;
    }
    return ans;
}
int f[105][105];
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    int a,b,ta=0,tb=0;
    init();
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a);
        addedge(0,i,a);
        ta+=a;
    }
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&b);
        addedge(i+n,2*n+1,b);
        tb+=b;
    }
    for(int i=1;i<=n;i++)
    {
        addedge(i,i+n,INF);
    }
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d",&a,&b);
        addedge(a,b+n,INF);
        addedge(b,a+n,INF);
    }
    int mf=sap(0,2*n+1,2*n+2);
    int u,v;
    if(mf==ta && mf==tb)
    {
        printf("YES\n");
        //printf("%d\n",tol);
        for(int i=0;i<tol;i+=2)
        {
            v=edge[i].to;
            u=edge[i^1].to;
            if(u>=1 && u<=n)
            {
                f[u][v-n]=edge[i].flow;
            }
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                printf("%s%d",(j==1 ? "" : " "),f[i][j]);
            }
            printf("\n");
        }
    }
    else printf("NO\n");
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值