POJ 2125 Destroying The Graph

39 篇文章 0 订阅

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 +

Source

Northeastern Europe 2003, Northern Subregion
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

最小点权覆盖~

删除每个点的全部出边或者全部入边使图中没有边,显然是求最小点权覆盖。

把每个点拆成两个点i,i+n。

从S向i连边,边权为out[i];从i+n向T连边,边权为in[i];每条有向边起点向终点+n连边,边权为inf。然后跑最小割就好了。

至于最后的输出,与COGS 727 [网络流24题] 太空飞行计划简直是一模一样,直接模仿那道题写的。

注意cnt=1!


#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
using namespace std;
#define inf 999999999

int n,m,x,y,fi[205],w[100001],ne[100001],v[100001],cnt,dis[205],kk[205],ans,num;
queue<int> q;

int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0' || ch>'9') {if(ch=='-') f=-1;ch=getchar();}
	while(ch>='0' && ch<='9') {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
	return x*f;
}

void add(int u,int vv,int val)
{
	w[++cnt]=vv;ne[cnt]=fi[u];fi[u]=cnt;v[cnt]=val;
	w[++cnt]=u;ne[cnt]=fi[vv];fi[vv]=cnt;
}

bool bfs()
{
	memset(dis,0,sizeof(dis));
	q.push(0);dis[0]=1;
	while(!q.empty())
	{
		int k=q.front();q.pop();
		for(int i=fi[k];i;i=ne[i])
		  if(v[i]>0 && !dis[w[i]])
		  {
		  	 dis[w[i]]=dis[k]+1;q.push(w[i]);
		  }
	}
	return dis[n*2+1];
}

int findd(int u,int vv)
{
	if(u==n*2+1 || !vv) return vv;
	int kkz,tot=0;
	for(int &i=kk[u];i;i=ne[i])
	  if(dis[w[i]]==dis[u]+1 && v[i]>0 && (kkz=findd(w[i],min(v[i],vv-tot))))
	  {
	  	v[i]-=kkz;v[i^1]+=kkz;tot+=kkz;
	  	if(tot==vv) return tot;
	  }
	return tot;
}

int main()
{
	n=read();m=read();cnt=1;
	for(int i=1;i<=n;i++) x=read(),add(i+n,n*2+1,x);
	for(int i=1;i<=n;i++) x=read(),add(0,i,x);
	for(int i=1;i<=m;i++) x=read(),y=read(),add(x,y+n,inf);
	while(bfs())
	{
		memcpy(kk,fi,sizeof(fi));
		ans+=findd(0,inf);
	}
	for(int i=1;i<=n;i++)
	{
		if(!dis[i]) num++;
		if(dis[i+n]) num++;
	}
	printf("%d\n%d\n",ans,num);
	for(int i=1;i<=n;i++)
	{
		if(!dis[i]) printf("%d -\n",i);
		if(dis[i+n]) printf("%d +\n",i);
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值