poj 3469 Dual Core CPU

一 原题

Dual Core CPU
Time Limit: 15000MS Memory Limit: 131072K
Total Submissions: 25359 Accepted: 10962
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: 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个任务和2个CPU,每个任务在两个CPU上执行分别需要ai, bi的时间,同时有一些任务最好在同一个CPU上完成,如果分开了,就会产生额外的时间。求最少需要多少时间完成所有任务。

并不难想到,一组任务分配的时间代价等价于一个图的割,于是可以用最大流算法解决这题。建图不复杂,但这题还是很容易TLE的,下面是我多路增广的Dinic算法实现。所谓多路增广就是在Dinic算法中dfs找最短增广路径时,一次性把所有当前长度的路径全部增广。详见代码。

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

const int maxn = 2e4 + 10;
const int maxm = 2e6 + 10;
const int inf = 0x7fffffff;

int n, m, s, t;
vector<int> g[maxn];

struct Edge {
	int src, dst, val;
	Edge() {}
	Edge(int s, int d, int v) : src(s), dst(d), val(v) {}
}edges[maxm];
int eid;

int hd, tl, q[maxn], dep[maxn];

void addEdge(int s, int d, int v, bool flag) {
	edges[eid++] = Edge(s, d, v);
	if(flag)
		edges[eid++] = Edge(d, s, v);
	else
		edges[eid++] = Edge(d, s, 0);
	g[s].push_back(eid - 2);
	g[d].push_back(eid - 1);
}

bool bfs() {
	for(int i = 0; i < maxn; i++) dep[i] = 0;
	hd = tl = 0;
	q[tl++] = s;
	dep[s] = 1;
	while(hd != tl) {
		int x = q[hd++];
		if(x == t) return true;
		for(int i = 0; i < g[x].size(); i++) {
			Edge e = edges[g[x][i]];
			if(e.val <= 0 || dep[e.dst] != 0) continue;
			dep[e.dst] = dep[x] + 1;
			q[tl++] = e.dst;
		}
	}
	return false;
}

int dfs(int cur, int flow) {
	if(cur == t) return flow;
	int ret = 0;
	for(int i = 0; i < g[cur].size(); i++) {
		Edge e = edges[g[cur][i]];
		if(dep[e.dst] != dep[cur] + 1 || e.val <= 0) continue;
		int delta = dfs(e.dst, min(flow, e.val));
		if(delta > 0) {
			edges[g[cur][i]].val -= delta;
			edges[g[cur][i] ^ 1].val += delta;
			ret += delta;
			flow -= delta;
			if(flow <= 0) break;
		}
	}
	return ret;
}

int dinic() {
	int ret = 0, delta;
	while(bfs())
		ret += dfs(s, inf);
	return ret;
}

int main() {
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	while(cin >> n >> m) {
		s = 0; t = n + 1; eid = 0;
		for(int i = 0; i < maxn; i++) g[i].clear();
		int a, b, c;
		for(int i = 1; i <= n; i++) {
			cin >> a >> b;
			addEdge(s, i, a, false);
			addEdge(i, t, b, false);
		}
		for(int i = 0; i < m; i++) {
			cin >> a >> b >> c;
			addEdge(a, b, c, true);
		}
		cout << dinic() << endl;
	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值