POJ3469 Dual Core CPU 最小割

这里我再给出几个等价关系
1:f是容量网络G的最大流。
2:|f|等于容量网络最小割的容量。
3:容量网络不存在增广路。
4:残留网络不存在从源点到汇点的路径。
以上四个描述等价。
这样我们就知道要求最小割就是要求一个最大流
另外我们要知道割的容量只计算前向弧。
 
建图:每个任务看做一个点,与源点连一条弧容量为消耗1,与汇点连一条弧容量为消耗2。
            额外消耗:任务a,b不在一处工作则要产生额外消耗,这里分别建两条弧,(a,b) 和(b,a)容量都为额外消耗。(割的容量只计算前向弧)
 
 
 
 
Dual Core CPU
Time Limit: 15000MS Memory Limit: 131072K
Total Submissions: 14117 Accepted: 6055
Case Time Limit: 5000MS

Description

As more and more computers are equipped with dual core CPU, SetagLilb, the Chief Technology Officer of TinySoft Corporation, decided to update their famous product - SWODNIW.

The routine consists of N modules, and each of them should run in a certain core. The costs for all the routines to execute on two cores has been estimated. Let's define them asAi andBi. Meanwhile, M pairs of modules need to do some data-exchange. If they are running on the same core, then the cost of this action can be ignored. Otherwise, some extra cost are needed. You should arrange wisely to minimize the total cost.

Input

There are two integers in the first line of input data, N and M (1 ≤N ≤ 20000, 1 ≤M ≤ 200000) .
The next N lines, each contains two integer, Ai and Bi.
In the following M lines, each contains three integers: a, b, w. The meaning is that if module a and module b don't execute on the same core, you should pay extraw dollars for the data-exchange between them.

Output

Output only one integer, the minimum total cost.

Sample Input

3 1
1 10
2 10
10 3
2 3 1000

Sample Output

13
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

#define INF 0xFFFFFF
#define MAXN 25010
#define MAXM 1000000

struct node
{
	int to,c;
	int next;
}e[MAXM];

int n,m,en,dis[MAXN],gap[MAXN];
int st,ed;
int g[MAXN];

void add(int a,int b,int c)
{
	e[en].to=b;
	e[en].c=c;
	e[en].next=g[a];
	g[a]=en++;
	e[en].to=a;
	e[en].c=0;
	e[en].next=g[b];
	g[b]=en++;	
}

int sap(int u,int flow)
{
	if(u==ed)
		return flow;
	int ans=0,i,t;
	for(i=g[u];i!=-1;i=e[i].next)
	{
		if( e[i].c && dis[u]==dis[e[i].to]+1)
		{
			t=sap(e[i].to,min(flow-ans,e[i].c));
			e[i].c-=t,e[i^1].c+=t,ans+=t;
			if(ans==flow)
				return ans;
		}
	}
	if(dis[st]>=n+2)
		return ans;
	if(!--gap[dis[u]])  //gap优化 
		dis[st]=n+2;
	++gap[++dis[u]];
	return ans;
}

void init()
{
	int a,b,c;
	en=0;
	for(int i=0;i<=n+1;i++)
	{
		gap[i]=dis[i]=0;
		g[i]=-1;
	}
	for(int i=1;i<=n;i++)
	{
		scanf("%d%d",&a,&b);
		add(0,i,a);
		add(i,n+1,b);
	}
	for(int i=1;i<=m;i++)
	{
		scanf("%d%d%d",&a,&b,&c);
		add(a,b,c);
		add(b,a,c);
	}
	st=0,ed=n+1;
}

void solve()
{
	int ans=0;
	for(gap[0]=n+2;dis[st]<n+2;)
		ans+=sap(st,INF);
	printf("%d\n",ans);
}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值