zoukankan      html  css  js  c++  java
  • 图论(网络流,二分图最小点权覆盖):POJ 2125 Destroying The Graph

    Destroying The Graph
     

    Description

    Alice and Bob play the following game. First, Alice draws some directed graph with N vertices and M arcs. After that Bob tries to destroy it. In a move he may take any vertex of the graph and remove either all arcs incoming into this vertex, or all arcs outgoing from this vertex.
    Alice assigns two costs to each vertex: Wi+ and Wi-. If Bob removes all arcs incoming into the i-th vertex he pays Wi+ dollars to Alice, and if he removes outgoing arcs he pays Wi- dollars.
    Find out what minimal sum Bob needs to remove all arcs from the graph.

    Input

    Input file describes the graph Alice has drawn. The first line of the input file contains N and M (1 <= N <= 100, 1 <= M <= 5000). The second line contains N integer numbers specifying Wi+. The third line defines Wi- in a similar way. All costs are positive and do not exceed 106 . Each of the following M lines contains two integers describing the corresponding arc of the graph. Graph may contain loops and parallel arcs.

    Output

    On the first line of the output file print W --- the minimal sum Bob must have to remove all arcs from the graph. On the second line print K --- the number of moves Bob needs to do it. After that print K lines that describe Bob's moves. Each line must first contain the number of the vertex and then '+' or '-' character, separated by one space. Character '+' means that Bob removes all arcs incoming into the specified vertex and '-' that Bob removes all arcs outgoing from the specified vertex.

    Sample Input

    3 6
    1 2 3
    4 2 1
    1 2
    1 1
    3 2
    1 2
    3 1
    2 3
    

    Sample Output

    5
    3
    1 +
    2 -
    2 +

      
      1 #include <iostream>
      2 #include <cstring>
      3 #include <cstdio>
      4 #include <queue>
      5 using namespace std;
      6 const int maxn=1010;
      7 const int maxm=100010;
      8 const int INF=1000000000;
      9 int cnt,tot,fir[maxn],fron[maxn],dis[maxn];
     10 int to[maxm],nxt[maxm],gap[maxn],path[maxn];
     11 int cap[maxm];queue<int>q;
     12 
     13 struct Max_Flow{
     14     void Init(int tot_=0){
     15         tot=tot_;cnt=1;
     16         memset(fir,0,sizeof(fir));
     17         memset(dis,0,sizeof(dis));
     18         memset(gap,0,sizeof(gap));
     19     }
     20     
     21     void add(int a,int b,int c){
     22         nxt[++cnt]=fir[a];
     23         fir[a]=cnt;
     24         cap[cnt]=c;
     25         to[cnt]=b;
     26     }
     27     
     28     void addedge(int a,int b,int c){
     29         add(a,b,c);
     30         add(b,a,0);
     31     }
     32     
     33     bool BFS(int s,int t){
     34         dis[t]=1;q.push(t);
     35         while(!q.empty()){
     36             int x=q.front();q.pop();
     37             for(int i=fir[x];i;i=nxt[i])
     38                 if(!dis[to[i]]){
     39                     dis[to[i]]=dis[x]+1;
     40                     q.push(to[i]);
     41                 }
     42         }
     43         return dis[s];
     44     }
     45     
     46     int Aug(int s,int t,int &p){
     47         int f=INF;
     48         while(p!=s){
     49             f=min(f,cap[path[p]]);
     50             p=to[path[p]^1];
     51         }p=t;
     52         while(p!=s){
     53             cap[path[p]]-=f;
     54             cap[path[p]^1]+=f;
     55             p=to[path[p]^1];
     56         }
     57         return f;
     58     }
     59     
     60     int ISAP(int s,int t){
     61         if(!BFS(s,t))return 0;
     62         for(int i=s;i<=t;i++)fron[i]=fir[i];
     63         for(int i=s;i<=t;i++)gap[dis[i]]+=1;
     64         int p=s,ret=0;
     65         while(dis[s]<=tot){
     66             if(p==t)ret+=Aug(s,t,p);
     67             
     68             for(int &i=fron[p];i;i=nxt[i])
     69                 if(cap[i]&&dis[p]==dis[to[i]]+1){
     70                     path[p=to[i]]=i;
     71                     break;
     72                 }
     73             
     74             if(!fron[p]){
     75                 if(--gap[dis[p]]==0)
     76                     break;
     77                 int Min=tot;
     78                 for(int i=fir[p];i;i=nxt[i])
     79                     if(cap[i])Min=min(Min,dis[to[i]]);
     80                 gap[dis[p]=Min+1]+=1;fron[p]=fir[p];
     81                 if(p!=s)p=to[path[p]^1];    
     82             }    
     83         }
     84         return ret;
     85     }
     86 }isap;
     87 
     88 int n,m,top;
     89 int tag[maxn],st[maxn];
     90 void DFS(int x){
     91     tag[x]=1;
     92     for(int i=fir[x];i;i=nxt[i])
     93         if(cap[i]&&!tag[to[i]])DFS(to[i]);
     94 }
     95 
     96 int main(){
     97     scanf("%d%d",&n,&m);
     98     int s=0,t=2*n+1;
     99     isap.Init(t+1);
    100     for(int i=1,v;i<=n;i++){
    101         scanf("%d",&v);
    102         isap.addedge(s,i,v);
    103     }
    104     for(int i=1,v;i<=n;i++){
    105         scanf("%d",&v);
    106         isap.addedge(i+n,t,v);
    107     }
    108     for(int i=1,a,b;i<=m;i++){
    109         scanf("%d%d",&a,&b);
    110         isap.addedge(b,a+n,INF);
    111     }
    112     
    113     printf("%d
    ",isap.ISAP(s,t));
    114     DFS(0);
    115     for(int i=1;i<=n;i++){
    116         if(!tag[i])
    117             st[++top]=i;
    118         if(tag[i+n])
    119             st[++top]=i+n;
    120     }
    121     printf("%d
    ",top);
    122     for(int i=1;i<=top;i++){
    123         if(st[i]<=n)
    124             printf("%d +
    ",st[i]);
    125         else
    126             printf("%d -
    ",st[i]-n);    
    127     }
    128     return 0;
    129 }
  • 相关阅读:
    BZOJ4503 两个串
    【挖坟】HDU3205 Factorization
    webpack打包 The 'mode' option has not been set, webpack will fallback to
    echarts js报错 Cannot read property 'getAttribute' of null
    微信支付 get_brand_wcpay_request fail,Undefined variable: openid
    layui动态设置checbox选中状态
    layui 获取radio单选框选中的值
    js 获取数组最后一个元素
    js生成指定范围内的随机数
    layer重复弹出(layui弹层同时存在多个)的解决方法
  • 原文地址:https://www.cnblogs.com/TenderRun/p/5651711.html
Copyright © 2011-2022 走看看