codeforces 546e Soldier and Traveling

来源:互联网 发布:unity3d控制物体跳跃 编辑:程序博客网 时间:2024/05/18 02:45

这里写链接内容
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

给定一些双向边 给定一些城市的初始人数 再给定一些城市的最终人数 询问能否把这些驻扎的士兵变成新的状态 那我只需要把每个城市拆点 然后每个点都向自己连一条inf的边 然后能互相到达的点直接连上inf的边 注意判断一些条件 比如我初始状态下 两个数组的总数是否相同 如果不相同就直接输出no否则打印一下边的权值即可

#include<cstdio>#include<vector>#include<cstring>#include<queue>#define N 220#define inf 0x3f3f3f3f#include<algorithm>using namespace std;inline char gc(){    static char now[1<<16],*S,*T;    if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}    return *S++;}inline int read(){    int x=0;char ch=gc();    while(ch<'0'||ch>'9') ch=gc();    while(ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=gc();}    return x;}struct node{    int y,z,next,x;}data[N*N*8];int num=1,h[N],level[N],T,n,m,mp[N][N];inline void insert1(int x,int y,int z){    data[++num].y=y;data[num].z=z;data[num].next=h[x];h[x]=num;data[num].x=x;    data[++num].y=x;data[num].z=0;data[num].next=h[y];h[y]=num;data[num].x=y;}inline bool bfs(){    queue<int>q;memset(level,0,sizeof(level));level[0]=1;q.push(0);    while(!q.empty()){        int x=q.front();q.pop();        for (int i=h[x];i;i=data[i].next){            int y=data[i].y,z=data[i].z;            if(level[y]||!z) continue;level[y]=level[x]+1;q.push(y);if (y==T) return 1;        }    }return  0;}inline int dfs(int x,int s){    if (x==T) return s;int ss=s;    for (int i=h[x];i;i=data[i].next){        int y=data[i].y,z=data[i].z;        if (level[x]+1==level[y]&&z){            int xx=dfs(y,min(s,z));if (!x) level[y]=0;            s-=xx;data[i].z-=xx;data[i^1].z+=xx;if (!s) return ss;        }    }return ss-s;}int main(){//freopen("cf.in","r",stdin);    n=read();m=read();T=2*n+1;int sum1=0,sum2=0;    for (int i=1;i<=n;++i) {        int tmp=read();insert1(0,i,tmp);sum1+=tmp;insert1(i,i+n,inf);    }    for (int i=1;i<=n;++i) {        int tmp=read();insert1(i+n,T,tmp);sum2+=tmp;    }    for (int i=1;i<=m;++i){        int x=read(),y=read();        insert1(x,y+n,inf);insert1(y,x+n,inf);    }int ans=0;while(bfs()) ans+=dfs(0,inf);//printf("%d %d %d\n",ans,sum1,sum2);    if (ans!=sum1||sum1!=sum2) {printf("NO");return 0;}    printf("YES\n");//  for (int i=2;i<=num;++i) printf("%d %d %d\n",data[i].x,data[i].y,data[i].z);    for (int i=1;i<=n;++i){        for (int j=h[i];j;j=data[j].next){            int y=data[j].y;if (y>n&&y<=2*n) mp[i][y-n]+=data[j^1].z;        }    }    for (int i=1;i<=n;++i){        for(int j=1;j<=n;++j) printf("%d ",mp[i][j]);printf("\n");    }    return 0;}
阅读全文
0 0
原创粉丝点击