dinic-546Ecf

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 ≤ 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.

Examples
input
Copy
4 4
1 2 6 3
3 5 3 1
1 2
2 3
3 4
4 2
output
Copy
YES
1 0 0 0
2 0 0 0
0 5 1 0
0 0 2 1
input
Copy
2 0
1 2
2 1
output
Copy
NO

 题意为对一个无向图,每个点的值可移动到相邻点(不可以移动到不相邻的点),问能否修改成所求的值。

代码来自:

https://blog.csdn.net/Icefox_zhx/article/details/78882732

把每个点和目标点分开来,原先的点连接源点,目标点连接汇点。跑最大流即可。

 

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define N 300
int read(){//快读
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x*f;
}
int n,m,a[N],h[N],num=1,T=210,tot=0,tot0=0,lev[N],cur[N],ans=0;
struct edge{
    int to,next,val;
}data[2000];
void add(int x,int y,int val){//因为是无向图,采用邻接表储存的时候同时储存反向边
    data[++num].to=y;data[num].next=h[x];h[x]=num;data[num].val=val;
    data[++num].to=x;data[num].next=h[y];h[y]=num;data[num].val=0;
}
bool bfs(){
    queue<int>q;
    memset(lev,0,sizeof(lev));
    q.push(0);
    lev[0]=1;
    while(!q.empty()){
        int x=q.front();
        q.pop();
        for(int i=h[x];i;i=data[i].next){
            if(lev[data[i].to]||!data[i].val) continue;
            int y=data[i].to;
            lev[y]=lev[x]+1;
            q.push(y);
        }
    }
    return lev[T];
}
int dinic(int x,int low){
    if(x==T) return low;
    int tmp=low;
    for(int &i=cur[x];i;i=data[i].next){
        int y=data[i].to;
        if(lev[y]!=lev[x]+1||!data[i].val) continue;
        int res=dinic(y,min(tmp,data[i].val));
        if(!res) lev[y]=0;
        else tmp-=res,data[i].val-=res,data[i^1].val+=res;//i^1即是反向边
        if(!tmp) return low;
    }
    return low-tmp;
}
int main(){
//  freopen("a.in","r",stdin);
    n=read();
    m=read();
    for(int i=1;i<=n;++i){
        int x=read();
        tot+=x;
        add(0,i,x);//连接源点
        add(i,i+n,inf);//连接目标点
    }
    for(int i=1;i<=n;++i){
        int x=read();
        tot0+=x;
        add(i+n,T,x);//连接汇点
    }
    if(tot!=tot0){//如果原先的总值和目标的总值不一样,一定无解
        puts("NO");
        return 0;
    }
    while(m--){
        int x=read(),y=read();
        add(x,n+y,inf);
        add(y,n+x,inf);//连接边
    }
    while(bfs()){
        memcpy(cur,h,sizeof(cur));
        ans+=dinic(0,inf);
    }
    if(ans!=tot){
        puts("NO");
        return 0;
    }
    puts("YES");
    for(int x=1;x<=n;++x){
        memset(a,0,sizeof(a));
        for(int i=h[x];i;i=data[i].next){
            if(!data[i].to) continue;
            int y=data[i].to-n;
            a[y]=data[i^1].val;
        }
        for(int i=1;i<n;++i) printf("%d ",a[i]);
        printf("%d\n",a[n]);
    }return 0;
}

 

转载于:https://www.cnblogs.com/wengsy150943/p/11262794.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值