Codeforces 546 E Soldier and Traveling【最大流Dinic+判断残余网络】

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.

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

 

题目大意:

有n个城市,有m条无向边,接下来第一行,n个数,表示每个城市现在有的士兵的数量,再接下来一行,n个数,表示每个城市需要有的士兵的数量,不能多也不能少,而且每个城市的士兵要么原地不动,要么只能移动到其直接相连的城市去(个数不限),问是否有可行方案,如果有可行方案输出出来。


思路:


1、经典最大流的模型,建图方式如下:

①建立源点,将源点连入各个城市,其权值设定为该城市拥有的士兵的数量。

②建立汇点,将各个城市连入汇点,其权值设定为该城市需要有的士兵的数量。

③将m条无向边加入图中,权值设定为INF。


2、然后跑一遍最大流,首先判断是否满流,如果满流都做不到,一定不可能分配成功,如果满流,那么继续判断其残余网络:

①对应残余网络我们分析那m条无向边(只考虑从源点指向汇点那条边就行,不用考虑退回边),对应如果其权值已经小于了INF,那么说明在流的分配的过程中,这条边有流经过,其减少的量,就是从其这条边的起点(士兵要移动了),到其终点分配的士兵的个数。

②那么我们枚举出所有边(不考虑退回边),将其权值减少的量就设定为最终输出的解。

③设定完毕之后,我们还有一道过程需要判断,是否最终的满流能够使得恰好分配到城市i的人数为b【i】个,如果所有城市都对上了,输出n*n的矩阵即可。


Ac代码:


#include<stdio.h>
#include<string.h>
#include<queue>
#include<iostream>
using namespace std;
struct node
{
    int from;
    int to;
    int next;
    int w;
}e[151515];
int divv[5000];
int head[5000];
int cur[5000];
int ans[105][105];
int map[105][105];
int a[105];
int b[105];
int n,m,cont,ss,tt;
void add(int from,int to,int w)
{
    e[cont].from=from;
    e[cont].to=to;
    e[cont].w=w;
    e[cont].next=head[from];
    head[from]=cont++;
}
void getmap()
{
    ss=n*2+1;
    tt=ss+1;
    cont=0;
    memset(head,-1,sizeof(head));
    for(int i=0;i<n;i++)
    {
        add(ss,i+1,a[i]);
        add(i+1,ss,0);
        add(i+1+n,tt,b[i]);
        add(tt,i+1+n,0);
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(map[i][j]==1)
            {
                add(i,j+n,0x3f3f3f3f);
                add(j+n,i,0);
            }
        }
        add(i,i+n,0x3f3f3f3f);
        add(i+n,i,0);
    }
}
int makedivv()
{
    memset(divv,0,sizeof(divv));
    divv[ss]=1;
    queue<int >s;
    s.push(ss);
    while(!s.empty())
    {
        int u=s.front();
        if(u==tt)return 1;
        s.pop();
        for(int i=head[u];i!=-1;i=e[i].next)
        {
            int v=e[i].to;
            int w=e[i].w;
            if(divv[v]==0&&w)
            {
                divv[v]=divv[u]+1;s.push(v);
            }
        }
    }
    return 0;
}
int Dfs(int u,int maxflow,int tt)
{
    if(u==tt)return maxflow;
    int ret=0;
    for(int &i=cur[u];i!=-1;i=e[i].next)
    {
        int w=e[i].w;
        int v=e[i].to;
        if(w&&divv[v]==divv[u]+1)
        {
            int f=Dfs(v,min(maxflow-ret,w),tt);
            e[i].w-=f;
            e[i^1].w+=f;
            ret+=f;
            if(ret==maxflow)return ret;
        }
    }
    return ret;
}
int Dinic()
{
    int ans=0;
    while(makedivv()==1)
    {
        memcpy(cur,head,sizeof(head));
        ans+=Dfs(ss,0x3f3f3f3f,tt);
    }
    return ans;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        int sum=0;
        memset(map,0,sizeof(map));
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
            sum+=a[i];
        }
        for(int i=0;i<n;i++)
        {
            scanf("%d",&b[i]);
        }
        for(int i=0;i<m;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            map[x][y]=1;
            map[y][x]=1;
        }
        getmap();
        int tmp=Dinic();
        if(tmp==sum)
        {
            for(int i=0;i<cont;i++)
            {
                if(i%2==0)
                {
                    if(e[i].from==ss||e[i].to==tt)continue;
                    else
                    {
                        //printf("%d\n",0x3f3f3f3f-e[i].w);
                        ans[e[i].from][e[i].to-n]=0x3f3f3f3f-e[i].w;
                    }
                }
            }
            int flag=0;
            for(int i=1;i<=n;i++)
            {
                int tmppp=0;
                for(int j=1;j<=n;j++)
                {
                    tmppp+=ans[j][i];
                }
                if(tmppp==b[i-1])continue;
                else flag=1;
            }
            if(flag==1)
            {
                printf("NO\n");
                continue;
            }
            printf("YES\n");
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=n;j++)
                {
                    printf("%d ",ans[i][j]);
                }
                printf("\n");
            }
        }
        else printf("NO\n");
    }
}











评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值