poj--3469--Dual Core CPU(最小割裸题)


Time Limit: 15000MS Memory Limit: 131072KB 64bit IO Format: %I64d & %I64u

 Status

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 as Ai and Bi. 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: abw. The meaning is that if module a and module b don't execute on the same core, you should pay extra w 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

Source

双核的电脑,两个核共由n个模块组成,每个模块放在1或者2都会产生一定的花费,并且有些模块如果在一个核中会产生额外的花费,问,将这n个模块放在一起需要的最少的话费
比较裸的最小割,两个核就是两个集合,直接套模板
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAXN 20010
#define INF 0x3f3f3f3f
struct node
{
	int u,v,cap,flow,next;
}edge[MAXN*100];
int vis[MAXN],dis[MAXN],n,m,head[MAXN],cnt,cur[MAXN];
void add(int a,int b,int c)
{
	node E={a,b,c,0,head[a]};
	edge[cnt]=E;
	head[a]=cnt++;
	node E1={b,a,0,0,head[b]};
	edge[cnt]=E1;
	head[b]=cnt++;
}
void getmap()
{
	int a,b,c;
	for(int i=1;i<=n;i++)
	{
		scanf("%d%d",&a,&b);
		add(0,i,a);
		add(i,1+n,b);
	}
	for(int i=0;i<m;i++)
	{
		scanf("%d%d%d",&a,&b,&c);
		add(a,b,c);
		add(b,a,c);
	}
}
bool bfs(int s,int e)
{
	queue<int>q;
	memset(vis,0,sizeof(vis));
	memset(dis,-1,sizeof(dis));
	while(!q.empty()) q.pop();
	q.push(s);
	dis[s]=0;
	vis[s]=1;
	while(!q.empty())
	{
		int u=q.front();
		q.pop();
		for(int i=head[u];i!=-1;i=edge[i].next)
		{
			node E=edge[i];
			if(E.cap>E.flow&&!vis[E.v])
			{
				vis[E.v]=1;
				dis[E.v]=dis[E.u]+1;
				if(E.v==e) return true;
				q.push(E.v);
			}
		}
	}
	return false;
}
int dfs(int x,int a,int e)
{
	if(x==e||a==0)
	return a;
	int flow=0,f;
	for(int &i=cur[x];i!=-1;i=edge[i].next)
	{
		node &E=edge[i];
		if(dis[x]+1==dis[E.v]&&(f=dfs(E.v,min(E.cap-E.flow,a),e))>0)
		{
			E.flow+=f;
			edge[i^1].flow-=f;
			a-=f;
			flow+=f;
			if(a==0) break;
		}
	}
	return flow;
}
int MAXflow(int s,int e)
{
	int flow=0;
	while(bfs(s,e))
	{
		memcpy(cur,head,sizeof(head));
		flow+=dfs(s,INF,e);
	}
	return flow;
}
int main()
{
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		cnt=0;
		memset(head,-1,sizeof(head));
		getmap();
		printf("%d\n",MAXflow(0,1+n));
	}
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值