zoukankan      html  css  js  c++  java
  • POJ 2125 Destroying The Graph [最小割 打印方案]

    Destroying The Graph
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 8311   Accepted: 2677   Special Judge

    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 +

    Source

    Northeastern Europe 2003, Northern Subregion

    题意from mhy12345:给一张有向图,现在要选择一些点,删掉图中的所有边。具体操作为:选择点 i,可
    以选择删除从 i 出发的所有有向边或者进入 i 的所有有向边,分别有个代价 ini 和
    outi,求最小的代价删掉所有边。并输出删除方案。


    建图 S-- w+ -->in-->out-- w- -->T
    显然要拆成入点和出点,对于一条边,出点或者入点的花费花一个就可以了
     
    打印方案的话,从S做BFS,对于一条边一个顶点vis一个顶点没vis就是割集里的了
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    const int N=205,M=6005,INF=1e9;
    inline int read(){
        char c=getchar();int x=0,f=1;
        while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
        while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
        return x*f;
    }
    int n,m,u,v,s,t,w[N];
    struct edge{
        int v,c,f,ne;
    }e[M<<1];
    int cnt,h[N];
    inline void ins(int u,int v,int c){
        cnt++;
        e[cnt].v=v;e[cnt].c=c;e[cnt].f=0;e[cnt].ne=h[u];h[u]=cnt;
        cnt++;
        e[cnt].v=u;e[cnt].c=0;e[cnt].f=0;e[cnt].ne=h[v];h[v]=cnt;
    }
    int cur[N],d[N],vis[N];
    int q[N],head,tail;
    bool bfs(){
        head=tail=1;
        memset(vis,0,sizeof(vis));
        d[s]=1;vis[s]=1;q[tail++]=s;
        while(head!=tail){
            int u=q[head++];
            for(int i=h[u];i;i=e[i].ne){
                int v=e[i].v;
                if(!vis[v]&&e[i].c>e[i].f){
                    vis[v]=1;d[v]=d[u]+1;
                    q[tail++]=v;
                    if(v==t) return true;
                }
            }
        }
        return false;
    }
    int dfs(int u,int a){
        if(u==t||a==0) return a;
        int flow=0,f;
        for(int &i=cur[u];i;i=e[i].ne){
            int v=e[i].v;
            if(d[v]==d[u]+1&&(f=dfs(v,min(e[i].c-e[i].f,a)))>0){
                flow+=f;
                e[i].f+=f;
                e[((i-1)^1)+1].f-=f;
                a-=f;
                if(a==0) break;
            }
        }
        if(a) d[u]=-1;
        return flow;
    }
    int dinic(){
        int flow=0;
        while(bfs()){
            for(int i=s;i<=t;i++) cur[i]=h[i];
            flow+=dfs(s,INF);
        }
        return flow;
    }
    
    void bfsSol(){
        head=tail=1;
        memset(vis,0,sizeof(vis));
        q[tail++]=s;vis[s]=1;
        while(head!=tail){
            int u=q[head++];
            for(int i=h[u];i;i=e[i].ne){
                int v=e[i].v;
                if(!vis[v]&&e[i].c>e[i].f){
                    vis[v]=1;
                    q[tail++]=v;
                }
            }
        }
    }
    int cut[M];
    void solve(){
        int flow=dinic();
        printf("%d
    ",flow);
        bfsSol();
        int num=0;
        for(int i=1;i<=n;i++){
            if(!vis[i]) num++;
            if(vis[i+n]) num++;
        }
        printf("%d
    ",num);
        for(int i=1;i<=n;i++){
            if(!vis[i]) printf("%d +
    ",i);
            if(vis[i+n]) printf("%d -
    ",i);
        }
    }
    int main(){
        //freopen("in.txt","r",stdin);
        n=read();m=read();s=0;t=n+n+1;
        for(int i=1;i<=n;i++) w[i]=read(),ins(s,i,w[i]);
        for(int i=1;i<=n;i++) w[i+n]=read(),ins(i+n,t,w[i+n]);
        for(int i=1;i<=m;i++){
            u=read();v=read();
            ins(v,u+n,INF);
        }
        solve();
    }
     
     
     
  • 相关阅读:
    腾讯QQ服务器汇总表 及禁用QQ方法
    C#下一个SQL SERVER数据库操作类
    获取页面运行时间以及数据库查询次数
    GridView导出Excel
    使用FileUpload控件上传图片并自动生成缩略图、带文字和图片的水印图
    JS图片loading及放大查看效果(兼容IE,FF)
    .NET 开发人员十个必备工具
    jsp日期时间格式化输出
    MyEclipse6.5与Perforce的集成
    [转]firefox与IE的nextSibling
  • 原文地址:https://www.cnblogs.com/candy99/p/6347798.html
Copyright © 2011-2022 走看看