POJ 2125 最小割最大流

Destroying The Graph
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 6732 Accepted: 2120 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 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 +


题意:给定一个图,给出删除某一个点入边,出边的费用,要求用最小的费用删除掉所有的边,

要删除所有的边,对于每一个点,要么删除其入边,要么删除其出边,两个操作一定要执行一个,很显然是一个最小点权覆盖,

添加一个源点,一个汇点,把每一个点拆点,假如删除入边,则相当于保留出边,出点向汇点连边,删除出边,则保留入边,

源点向入点连边,对于每一条给定的边,起点的入点向终点的出点连边,求出的最大流就是最小费用,这时候图已经被分为两部分,然后在残图上dfs标记,

就可以找到操作方案。

代码:

/* ***********************************************
Author :rabbit
Created Time :2014/3/8 16:40:10
File Name :1718.cpp
************************************************ */
#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <string>
#include <time.h>
#include <math.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-5
#define pi acos(-1.0)
typedef long long ll;
const int maxn=100010;
const int maxm=400010;
struct Edge{
    int to,next,cap,flow;
    Edge(){};
    Edge(int _next,int _to,int _cap,int _flow){
        next=_next;to=_to;cap=_cap;flow=_flow;
    }
}edge[maxm];
int head[maxn],tol,gap[maxn],dep[maxn],cur[maxn];
void addedge(int u,int v,int flow){
    edge[tol]=Edge(head[u],v,flow,0);head[u]=tol++;
    edge[tol]=Edge(head[v],u,0,0);head[v]=tol++;
}
int Q[maxn];
void bfs(int start,int end){
    memset(dep,-1,sizeof(dep));
    memset(gap,0,sizeof(gap));
    gap[0]++;int front=0,rear=0;
    dep[end]=0;Q[rear++]=end;
    while(front!=rear){
        int u=Q[front++];
        for(int i=head[u];i!=-1;i=edge[i].next){
            int v=edge[i].to;if(dep[v]==-1&&edge[i].cap)
                Q[rear++]=v,dep[v]=dep[u]+1,gap[dep[v]]++;
        }
    }
}
int S[maxn];
int sap(int start,int end,int N){
    bfs(start,end);
    memcpy(cur,head,sizeof(head));
    int top=0,u=start,ans=0;
    while(dep[start]<N){
        if(u==end){
            int MIN=INF,id;
            for(int i=0;i<top;i++)
                if(MIN>edge[S[i]].cap-edge[S[i]].flow)
                    MIN=edge[S[i]].cap-edge[S[i]].flow,id=i;
            for(int i=0;i<top;i++)
                edge[S[i]].flow+=MIN,edge[S[i]^1].flow-=MIN;
            ans+=MIN,top=id,u=edge[S[top]^1].to;
            continue;
        }
        bool flag=0;int v;
        for(int i=cur[u];i!=-1;i=edge[i].next){
            v=edge[i].to;
            if(edge[i].cap-edge[i].flow&&dep[v]+1==dep[u]){
                flag=1;cur[u]=i;break;
            }
        }
        if(flag){
            S[top++]=cur[u];u=v;continue;
        }
        int MIN=N;
        for(int i=head[u];i!=-1;i=edge[i].next)
            if(edge[i].cap-edge[i].flow&&dep[edge[i].to]<MIN)
                MIN=dep[edge[i].to],cur[u]=i;
        if(--gap[dep[u]]==0)break;gap[dep[u]=MIN+1]++;
        if(u!=start)u=edge[S[--top]^1].to;
    }
    return ans;
}
int mark[maxn];
void dfs(int u){
	mark[u]=1;
	for(int i=head[u];i!=-1;i=edge[i].next){
		int v=edge[i].to;
		if(!mark[v]&&edge[i].cap-edge[i].flow)dfs(v);
	}
}
int main(){
	int i,j,k,m,n;
	while(~scanf("%d%d",&n,&m)){
		memset(head,-1,sizeof(head));tol=0;
		for( i=1;i<=n;i++){
			scanf("%d",&j);
			addedge(i+n,2*n+1,j);
		}
		for( i=1;i<=n;i++){
			scanf("%d",&j);
			addedge(0,i,j);
		}
		while(m--){
			scanf("%d%d",&i,&j);
			addedge(i,j+n,INF);
		}
		int cnt=sap(0,2*n+1,10*n);
		cout<<cnt<<endl;
		memset(mark,0,sizeof(mark));
		dfs(0);
		int ret=0;
		for( i=1;i<=n;i++){
			if(mark[i]==0)ret++;
			if(mark[i+n])ret++;
		}
		cout<<ret<<endl;
		for(i=1;i<=n;i++){
			if(mark[i]==0)cout<<i<<" -"<<endl;
			if(mark[i+n])cout<<i<<" +"<<endl;
		}
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值