poj 3469 Dual Core CPU

来源:互联网 发布:万达怎么了 知乎 编辑:程序博客网 时间:2024/06/18 10:04

一 原题

Dual Core CPU
Time Limit: 15000MS Memory Limit: 131072KTotal Submissions: 25359 Accepted: 10962Case 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 11 102 1010 32 3 1000

Sample Output

13

Source

POJ Monthly--2007.11.25, Zhou Dong


二 分析

题意:给定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);elseedges[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;}



原创粉丝点击