Dual Core CPU(最小割)

Dual Core CPU
Time Limit: 15000MS Memory Limit: 131072K
Total Submissions: 26136 Accepted: 11270
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 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: a, b, w. 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

POJ Monthly–2007.11.25, Zhou Dong

由于要将 n n n个点分成两组,那么这显然是“割”的意思,再仔细想想,如果我们建立源点 s s s和汇点 t t t,从 s s s向每个点连容量为 A i A_i Ai的边,从每个点向 t t t连容量为 B i B_i Bi的边,然后两个点 i , j i,j i,j连容量为 w w w的无向边,建好图后我们只用求出最小割即可。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#define N 250000
#define M 3000000
using namespace std;
inline long long read(){
	long long ans=0;
	char ch=getchar();
	while(!isdigit(ch))ch=getchar();
	while(isdigit(ch))ans=(ans<<3)+(ans<<1)+ch-'0',ch=getchar();
	return ans;
}
int n,m,s,t,ans=0,d[N],first[N],cnt=-1;
struct Node{int v,next,c;}e[M<<1];
inline void add(int u,int v,int c){
	e[++cnt].v=v;
	e[cnt].next=first[u];
	e[cnt].c=c;
	first[u]=cnt;
	e[++cnt].v=u;
	e[cnt].next=first[v];
	e[cnt].c=0;
	first[v]=cnt;
}
inline bool bfs(){
	queue<int>q;
	q.push(s);
	memset(d,-1,sizeof(d));
	d[s]=0;
	while(!q.empty()){
		int x=q.front();
		q.pop();
		for(int i=first[x];i!=-1;i=e[i].next){
			int v=e[i].v;
			if(d[v]!=-1||e[i].c<=0)continue;
			d[v]=d[x]+1;
			if(v==t)return true;
			q.push(v);
		}
	}
	return false;
}
inline int dfs(int x,int f){
	if(x==t||!f)return f;
	int flow=f;
	for(int i=first[x];i!=-1;i=e[i].next){
		int v=e[i].v;
		if(flow&&e[i].c>0&&d[v]==d[x]+1){
			int tmp=dfs(v,min(flow,e[i].c));
			if(!tmp)d[v]=-1;
			e[i].c-=tmp;
			e[i^1].c+=tmp;
			flow-=tmp;
		}
	}
	return f-flow;
}
int main(){
	memset(first,-1,sizeof(first));
	n=read(),m=read(),s=0,t=n+1;
	for(int i=1;i<=n;++i){
		int a=read(),b=read();
		add(s,i,a),add(i,t,b);
	}
	for(int i=1;i<=m;++i){
		int a=read(),b=read(),w=read();
		add(a,b,w),add(b,a,w);
	}
	while(bfs())ans+=dfs(s,0x3f3f3f3f);
	printf("%d",ans);
	return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值