poj2125Destroying The Graph【最小割】最小花费输出

来源:互联网 发布:淘宝商城河北 编辑:程序博客网 时间:2024/05/19 04:27

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 61 2 34 2 11 21 13 21 23 12 3

Sample Output

531 +2 -2 +

Source

Northeastern Europe 2003, Northern Subregion

又是拖了好久的题————

题意:

N个点M条边的有向图,给出如下两种操作。
删除点i的所有出边,代价是Ai。
删除点j的所有入边,代价是Bj。
求最后删除图中所有的边的最小代价。

做法:

看到这个形式的题设,很明显应该是左边拎出来一个源点,右边拎出来一个汇点,中间拆点。但要是不看题解,左右的边权就加反啦QAQ

加边的时候这么想:

已知的边是从左边流到右边的,从左边出来,左边点连得是outcome,进去右边,右边连得是income值

再说输出:

回忆网络流的过程,对待给定的flow[i]一直减去tmp,一直减到0的时候是满流,那么我搜索的就是非满流边,左侧没有走到的点是要输出的点;右侧走到的点是要输出的点,为什么呢?我搜索的边是非满流边,左边既然没有满流,右边一定的是满流的啊,所以右侧是遍历过的被输出

p.s.复习的时候发现说的还是不明白,画个图就懂了


#include <stdio.h>#include<cstring>#include <iostream>using namespace std;const int oo=0x3f3f3f3f;const int mm=111111;const int mn=2000;int node ,scr,dest,edge;int ver[mm],flow[mm],next[mm];int head[mn],work[mn],dis[mn],q[mn];void prepare(int _node,int _scr,int _dest){    node=_node,scr=_scr,dest=_dest;    for(int i=0; i<node; ++i)        head[i]=-1;    edge=0;}void addedge(int u,int v,int c){    ver[edge]=v,flow[edge]=c,next[edge]=head[u],head[u]=edge++;    ver[edge]=u,flow[edge]=0,next[edge]=head[v],head[v]=edge++;}bool Dinic_bfs(){    int i,u,v,l,r=0;    for(i=0; i<node; i++)        dis[i]=-1;    dis[q[r++]=scr]=0;    for(l=0; l<r; ++l)    {        for(i=head[u=q[l]]; i>=0; i=next[i])        {            if(flow[i]&&dis[v=ver[i]]<0)            {                dis[q[r++]=v]=dis[u]+1;                if(v==dest)                    return 1;            }        }    }    return 0;}int Dinic_dfs(int u,int exp){    if(u==dest)        return exp;    for(int &i=work[u],v,tmp; i>=0; i=next[i])        if(flow[i]&&dis[v=ver[i]]==dis[u]+1&&(tmp=Dinic_dfs(v,min(exp,flow[i])))>0)        {            flow[i]-=tmp;            flow[i^1]+=tmp;            return tmp;        }    return 0;}int Dinic_flow(){    int i,ret=0,delta;    while(Dinic_bfs())    {        for(i=0; i<node; i++)            work[i]=head[i];        while(delta=Dinic_dfs(scr,oo))            ret+=delta;    }    return ret;}bool vis[mn];void dfs(int u){    vis[u]=1;    for(int i=head[u];i!=-1;i=next[i])    {        if(!vis[ver[i]]&&flow[i])            dfs(ver[i]);    }}int main(){  //  freopen("cin.txt","r",stdin);    int n,m;    while(~scanf("%d%d",&n,&m))    {        prepare(2*n+2,0,2*n+1);        for(int i=1;i<=n;i++)        {            int a;            scanf("%d",&a);            addedge(i+n,dest,a);        }        for(int i=1;i<=n;i++)        {            int a;            scanf("%d",&a);            addedge(0,i,a);        }        for(int i=0;i<m;i++)        {            int u,v;            scanf("%d%d",&u,&v);            addedge(u,v+n,oo);        }        printf("%d\n",Dinic_flow());        memset(vis,0,sizeof(vis));        int ans=0;        dfs(0);        for(int i=1;i<=n;i++)            ans+=((!vis[i])+(vis[i+n]));        printf("%d\n",ans);        for(int i=1;i<=n;i++)        {            if(!vis[i])printf("%d -\n",i);            if(vis[i+n])printf("%d +\n",i);        }    }    return 0;}


0 0
原创粉丝点击