POJ2125 Destroying The Graph 二分图+最小点权覆盖+最小割

趁热打铁,再来几题,加深理解。
一样是求最小点权覆盖,同样求一个最小割,但是要求出割去了那些边,只要用最终的剩余网络进行一次遍历就可以了,比较简单。
建图:同样是一个二分图,左边的点代表去掉出边,右边的点代表去掉入边(小心别弄混),左边去掉出边的点与源点相连,容量为wi- 。右边的点与汇点相连,容量为wi+。然后更据给出的弧进行连线,权值为INF
Destroying The Graph
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 5485 Accepted: 1740 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 +
#include<iostream>
#include<cstdio>
#include<algorithm> 

using namespace std;

#define mm(a) memset((a),0,sizeof((a)))
#define INF 0xFFFFFF
#define MAXN 255

int n,m,c[MAXN][MAXN],dis[MAXN],gap[MAXN];
int st,ed;
bool visited[MAXN];
int stack[MAXN],top;


int sap(int u,int flow)
{
	if(u==ed) return flow;
	int ans=0,i,t;
	for(i=0;i<=n+1;++i)
		if(c[u][i]&&dis[u]==dis[i]+1)
		{
			t=sap(i,min(flow-ans,c[u][i]));
			c[u][i]-=t,c[i][u]+=t,ans+=t;
			if(ans==flow) return ans;
		}
	if(dis[st]>=n+2) return ans;
	if(!--gap[dis[u]]) dis[st]=n+2;
	++gap[++dis[u]];
	return ans;
}

void dfs(int p)
{
	if(visited[p]) return ;
	visited[p]=true;
	for(int i=0;i<n+1;i++)
		if(!visited[i] && c[p][i]) dfs(i);
}

void solve()
{
	int x,y,ans=0,temp;
	mm(dis),mm(gap),mm(c),mm(visited);
	for(int i=1;i<=n;i++) scanf("%d",&c[n+i][n*2+1]);
	for(int i=1;i<=n;i++) scanf("%d",&c[0][i]);
	for(int i=1;i<=m;i++)
	{
		scanf("%d%d",&x,&y);
		c[x][y+n]=INF;
	}
	n=n<<1;
	st=0,ed=n+1;
	for(gap[0]=n+2;dis[st]<n+2;) ans+=sap(st,INF);
	dfs(0);
	cout<<ans<<endl;
	top=0;
	n=n>>1;
	for(int i=1;i<=n;i++)
	{
		if(!visited[i]) stack[top++]=i;
		if(visited[i+n]) stack[top++]=i+n;
	}
	cout<<top<<endl;
	while(top>0)
	{
		temp=stack[--top];
		if(temp<=n) cout<<temp<<" -"<<endl;
		else cout<<temp-n<<" +"<<endl;
	}	
}

int main()
{
	while(scanf("%d%d",&n,&m)!=EOF)	solve();
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值