Dual Core CPU POJ - 3469 最大流 最小割 Dinic算法

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

题意:有n个模块要在A或B上运行, 费用分别是a[i]和b[i], 还有m个关系, 如果a[i]和b[i]不在同一个CPU上执行, 那么需要额外花费c[i]。 求最小花费。

思路:首先, 很显然的是, 要把模块分成两个集合, 有一些属于A, 有一些属于B,这种将对象划分成两个集合的问题, 我们常用最小割来解决,  那么对于每个模块, 如果它属于A, 为了割断 , 要将他与汇点连一条容量为a[i]的边, 反之, 与源点连。    对于额外费用, 只要模块a[i]向模块b[i]连一条容量为w[i]的边对应起来就行了。

为了便于理解, 我们可以把样例画出图来, 那么可以发现, 最小割取决于s到t的路径中最小的流量边。

#include<iostream>
#include<cstdio>
#include<string.h>
#include<vector>
#include<queue>
#include<cmath>
using namespace std;

const int maxv=1e6+10, maxn=1e2+10, INF=1e9;

struct edge{int to, cap, rev;};
vector<edge> G[maxv];
int level[maxv],iter[maxv];
bool used[maxv];

void add_edge(int from,int to,int cap)
{
	G[from].push_back((edge){to, cap, G[to].size()});
	G[to].push_back((edge){from, 0, G[from].size()-1});
}

void bfs(int s){
	memset(level, -1, sizeof(level));
	queue<int> que;
	level[s]=0;
	que.push(s);
	while (!que.empty())
	{
		int v=que.front();
		que.pop();
		for (int i=0; i<G[v].size(); i++)
		{
			edge &e=G[v][i];
			if (e.cap > 0 && level[e.to] < 0)
			{
				level[e.to] = level[v] + 1;
				que.push(e.to); 
			}
		}
	}
}

int dfs(int v, int t, int f)
{
	if (v==t) return f;
	for (int &i=iter[v]; i<G[v].size(); i++)
	{
		edge &e=G[v][i];
		if (e.cap > 0 && level[v] < level[e.to]){
			int d=dfs(e.to, t, min(f, e.cap));
			if (d>0){
				e.cap -= d;
				G[e.to][e.rev].cap+=d;
				return d;
			}			
		}
	}	
	return 0;
}

int max_flow(int s, int t){
	int flow=0;
	for (;;){
		bfs(s);
		if (level[t]<0) return flow;
		memset(iter, 0, sizeof(iter));
		int f;
		while ((f = dfs(s,t,INF))>0)
		flow += f;
	} 
} 

int main()
{
	int n,m,s,t,a,b,w;
		
	scanf("%d%d", &n, &m); 
		
		s=n+1; t=s+1;
		for (int i=1; i<=n; i++)
		{
			scanf("%d%d", &a, &b);
			add_edge(s, i, a);
			add_edge(i, t, b);
		}
		
		for (int i=0; i<m; i++)
		{
			scanf("%d%d%d", &a, &b, &w);
			add_edge(a, b, w);
			add_edge(b, a, w);
		}
		
		printf("%d", max_flow(s,t));

	return 0;
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值