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

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 10 6 . 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

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

题意:

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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值