zoukankan      html  css  js  c++  java
  • 网络流(最大流) CodeForces 546E:Soldier and Traveling

    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.

     

    Sample Input

    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
      水题。
      1 #include <iostream>
      2 #include <cstring>
      3 #include <cstdio>
      4 using namespace std;
      5 const int N=210,M=80010,INF=1000000000;
      6 int n,m,cnt,fir[N],nxt[M],to[M],cap[M];
      7 int dis[N],path[N],fron[N],gap[N],q[N],front,back;
      8 struct Net_Flow{
      9     void Init(){
     10         memset(fir,0,sizeof(fir));
     11         memset(dis,0,sizeof(dis));
     12         memset(gap,0,sizeof(gap));
     13         front=back=cnt=1;
     14     }
     15     void add(int a,int b,int c){
     16         nxt[++cnt]=fir[a];
     17         to[fir[a]=cnt]=b;
     18         cap[cnt]=c;
     19     }
     20     void addedge(int a,int b,int c)
     21         {add(a,b,c);add(b,a,0);}
     22     bool BFS(int S,int T){
     23         q[front]=T;dis[T]=1;
     24         while(front<=back){
     25             int x=q[front++];
     26             for(int i=fir[x];i;i=nxt[i])
     27                 if(!dis[to[i]])dis[q[++back]=to[i]]=dis[x]+1;
     28         }
     29         return dis[S];
     30     }
     31     int ISAP(int S,int T){
     32         if(!BFS(S,T))return 0;
     33         for(int i=S;i<=T;i++)gap[dis[i]]+=1;
     34         for(int i=S;i<=T;i++)fron[i]=fir[i];
     35         int ret=0,f,p=S,Min;
     36         while(dis[S]<=T+1){
     37             if(p==T){
     38                 f=INF;
     39                 while(p!=S){
     40                     f=min(f,cap[path[p]]);
     41                     p=to[path[p]^1];
     42                 }p=T;ret+=f;
     43                 while(p!=S){
     44                     cap[path[p]]-=f;
     45                     cap[path[p]^1]+=f;
     46                     p=to[path[p]^1];
     47                 }
     48             }
     49             for(int &i=fron[p];i;i=nxt[i])
     50                 if(cap[i]&&dis[to[i]]==dis[p]-1){
     51                     path[p=to[i]]=i;break;
     52                 }
     53             if(!fron[p]){
     54                 if(!--gap[dis[p]])break;Min=T+1;
     55                 for(int i=fir[p];i;i=nxt[i])
     56                     if(cap[i])Min=min(Min,dis[to[i]]);
     57                 ++gap[dis[p]=Min+1];fron[p]=fir[p];
     58                 if(p!=S)p=to[path[p]^1];    
     59             }    
     60         }    
     61         return ret;
     62     }
     63     int G[N][N];
     64     void PRINT(){
     65         puts("YES");
     66         for(int x=1,y;x<=n;x++)
     67             for(int i=fir[x];i;i=nxt[i])
     68                 if((y=to[i])>n&&cap[i^1])
     69                     G[x][y-n]=cap[i^1];
     70         for(int x=1;x<=n;x++){
     71             for(int y=1;y<=n;y++)
     72                 printf("%d ",G[x][y]);
     73             puts("");    
     74         }
     75     }
     76 }isap;
     77 int S,T;
     78 int a[N],b[N];
     79 int main(){
     80     scanf("%d%d",&n,&m);
     81     isap.Init();T=2*n+1;
     82     for(int i=1;i<=n;i++){
     83         scanf("%d",&a[i]);a[0]+=a[i];
     84         isap.addedge(S,i,a[i]);
     85     }
     86     for(int i=1;i<=n;i++){
     87         scanf("%d",&b[i]);b[0]+=b[i];
     88         isap.addedge(i+n,T,b[i]);
     89         isap.addedge(i,i+n,INF);
     90     }
     91     while(m--){int a,b;
     92         scanf("%d%d",&a,&b);
     93         isap.addedge(a,b+n,INF);
     94         isap.addedge(b,a+n,INF);
     95     }
     96     if(a[0]!=b[0])puts("NO");
     97     else if(isap.ISAP(S,T)!=a[0])puts("NO");
     98     else isap.PRINT();
     99     return 0;
    100 }
  • 相关阅读:
    华科机考:特殊排序
    华科机考:排序
    华科机考:字符串连接
    华科机考:a+b
    华科机考:IP地址
    华科机考:统计单词
    iOS 应用评分
    jQuery Custom PopUp Window
    Csharp:字符串操作
    Css:Conditional comments 条件注释
  • 原文地址:https://www.cnblogs.com/TenderRun/p/5928212.html
Copyright © 2011-2022 走看看