欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

E

程序员文章站 2022-04-27 18:57:52
...

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

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
using namespace std;
#define LS 2*i
#define RS 2*i+1
#define UP(i,x,y) for(i=x;i<=y;i++)
#define DOWN(i,x,y) for(i=x;i>=y;i--)
#define MEM(a,x) memset(a,x,sizeof(a))
#define W(a) while(a)
#define LL long long
#define N 5005
#define MOD 19999997
#define INF 0x3f3f3f3f
#define EXP 1e-8
const double Pi = acos(-1.0);
vector<int> G[N];
int dis[N][N],n,m;
int vis[N];
int s1,s2,t1,t2,d1,d2;
int main()
{
    int i,j,k,x,y;
    cin>>n>>m;
    UP(i,1,m)
    {
        scanf("%d%d",&x,&y);
        G[x].push_back(y);
        G[y].push_back(x);
    }
    cin>>s1>>t1>>d1>>s2>>t2>>d2;
    MEM(dis,0);
    UP(i,1,n)
    {
        queue<int> Q;
        MEM(vis,0);
        vis[i] = 1;
        Q.push(i);
        W(!Q.empty())
        {
            int u = Q.front(),v;
            Q.pop();
           for(j = 0;j<G[u].size();j++)
            {
                v = G[u][j];
                if(vis[v]) continue;
                vis[v] = 1;
                dis[i][v] = dis[i][u]+1;
                Q.push(v);
            }
        }
    }
    if(dis[s1][t1]>d1||dis[s2][t2]>d2)
    {
        printf("-1\n");
        return 0;
    }
    int ans = dis[s1][t1]+dis[s2][t2];
    UP(i,1,n)
    {
        UP(j,1,n)
        {
            if(dis[s1][i]+dis[i][j]+dis[j][t1]<=d1&&dis[s2][i]+dis[i][j]+dis[j][t2]<=d2)
                ans=min(ans,dis[s1][i]+dis[i][j]+dis[j][t1]+dis[s2][i]+dis[j][t2]);
            if(dis[s1][i]+dis[i][j]+dis[j][t1]<=d1&&dis[t2][i]+dis[i][j]+dis[j][s2]<=d2)
                ans=min(ans,dis[s1][i]+dis[i][j]+dis[j][t1]+dis[t2][i]+dis[j][s2]);
        }
    }
    printf("%d\n",m-ans);
    return 0;
}
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<queue>
using namespace std;
const int N=1001;
struct node{
    int c,to,next;
}e[N<<1];
int num=1,head[N<<3],flag;
int dep[N<<3],n,m,a[N<<3],b[N<<3],sum,tot,s,t;
int map[N][N];
int read(){
    int x=0,w=1;char ch=getchar();
    while(ch>'9'||ch<'0'){if(ch=='-')w=-1;ch=getchar();}
    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    return x*w;
}

void add(int from,int to,int c){
    num++;
    e[num].to=to;
    e[num].c=c;
    e[num].next=head[from];
    head[from]=num;
}

bool bfs(){
    memset(dep,0,sizeof(dep));
    queue<int>q;q.push(s);dep[s]=1;
    while(!q.empty()){
        int u=q.front();q.pop();
        for(int i=head[u];i;i=e[i].next){
            int v=e[i].to;
            if(!dep[v]&&e[i].c){
                dep[v]=dep[u]+1;q.push(v);
            }
        }
    }
    return dep[t];
}

int dfs(int x,int cap){
    if(x==t)return cap;
    int addx=0;
    for(int i=head[x];i;i=e[i].next){
        int v=e[i].to;
        if(dep[v]==dep[x]+1&&e[i].c){
            int tmp=dfs(v,min(cap-addx,e[i].c));
            e[i].c-=tmp;e[i^1].c+=tmp;addx+=tmp;
        }
    }
    return addx;
}

int dinic(){
    int ans=0;
    while(bfs())ans+=dfs(s,10000000);
    return ans;
}

int main()
{
    n=read();m=read();s=0,t=n+n+1;
    for(int i=1;i<=n;i++)a[i]=read(),add(s,i,a[i]),add(i,s,0),tot+=a[i];
    for(int i=1;i<=n;i++)b[i]=read(),sum+=b[i],add(i+n,t,b[i]),add(t,i+n,0);
    for(int i=1;i<=n;i++)add(i,i+n,99999999),add(i+n,i,0);
    for(int i=1;i<=m;i++){
        int x=read(),y=read();
    //  if(x<y)swap(x,y);
        add(x,y+n,99999999);add(y+n,x,0);
        add(y,x+n,99999999);add(x+n,y,0);
    }
    //cout<<sum<<' '<<dinic();
    if(tot!=sum){printf("NO");return 0;}
    int ans=dinic();
    if(sum==ans){printf("YES\n");
        for(int i=1;i<=n;i++){
            for(int j=head[i];j;j=e[j].next){
                int v=e[j].to;
                if(v>n)
                map[i][v-n]=e[j^1].c;
            }
        }
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++)
            cout<<map[i][j]<<' ';
            cout<<endl;
        }
    }
    else printf("NO");
    return 0;
}