POJ 3469 Dual Core CPU

来源:互联网 发布:苹果编程语言swift 编辑:程序博客网 时间:2024/06/06 15:48
Language:
Dual Core CPU
Time Limit: 15000MS Memory Limit: 131072KTotal Submissions: 24901 Accepted: 10783Case 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
一CPUA为源点CPUB为汇点建图,对于每个模块从源点A向点i连一条容量为Ai的弧,从i向汇点B连一条容量为Bi的弧,
后m组每组点a和点b连一条容量为w的弧,由于每个模块无法同时在两个cpu下运行,问题转化为求图的最小割。
#include<stdio.h>#include<string.h>#include<queue>#include<algorithm>using namespace std;const int maxm = 1000005;const int INF = 0x3f3f3f3f;int W[maxm], V[maxm], head[maxm], Next[maxm], dep[30005], cur[30005];int n, m, s, t, cnt;void init(){cnt = -1, s = 0, t = n + 1;memset(head, -1, sizeof(head));memset(Next, -1, sizeof(Next));}void add(int u, int v, int w){cnt++;Next[cnt] = head[u];head[u] = cnt;V[cnt] = v;W[cnt] = w;}int bfs(){queue<int>q;memset(dep, 0, sizeof(dep));dep[s] = 1;q.push(s);while (!q.empty()){int u = q.front();q.pop();for (int i = head[u]; i != -1;i = Next[i]){if (dep[V[i]] == 0 && W[i]){dep[V[i]] = dep[u] + 1;q.push(V[i]);}}}if (dep[t] == 0) return 0;return 1;}int dfs(int u, int flow){if (u == t) return  flow;int minflow = 0;for (int i = cur[u];i != -1;i = Next[i]){if (dep[V[i]] == dep[u] + 1 && W[i] > 0){int d = dfs(V[i], min(W[i], flow));W[i] -= d;W[i ^ 1] += d;flow -= d;minflow += d;if (flow == 0) break;}}return minflow;}int dinic(){int ans = 0;while (bfs()){for (int i = 0;i <= n + 1;i++)cur[i] = head[i];ans += dfs(s, INF);}return ans;}int main(){int x, y, z, i, m;scanf("%d%d", &n, &m);init();for (i = 1;i <= n;i++){scanf("%d%d", &x, &y);add(s, i, x), add(i, s, 0);add(i, t, y), add(t, i, 0);}for (i = 1;i <= m;i++){scanf("%d%d%d", &x, &y, &z);add(x, y, z), add(y, x, z);}printf("%d\n", dinic());return 0;}