【最小割】POJ-3469 Dual Core CPU

34 篇文章 0 订阅
30 篇文章 0 订阅
Dual Core CPU
Time Limit: 15000MS Memory Limit: 131072K
   
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
————————————————————兴奋的分割线————————————————————
前言:竟然1A了~~嗨森
思路:传说中的最小割。首先要知道最小割最大流定理。然后只要能建对图,只剩下套模板了。
一开始想了很久。。。毕竟题目问的是“最小花费”,有点难想。
不如直接从“最小割”下手!根据题意、样例1容易得到所有的解:
A1 + A2 + A3 = 1 + 2 + 10 = 13……………………..①
A1 + A2 + B3 = 1 + 2 + 1000 + 3 = 1006
A1 + B2 + A3 = 1 + 10 + 1000 + 10 = 1021
A1 + B2 + B3 = 1 + 10 + 3 = 14
B1 + A2 + A3 = 略......
B1 + A2 + B3 = 
B1 + B2 + A3 = 
B1 + B2 + B3 = 10 + 10 + 3 = 23……………………②
这就是所有的割了!注意到一条割仅包含三个元素。而且注意到①和②的特殊情况是:
全在A号CPU; 全在B号CPU。
不妨把A当做源点S,B当做汇点T,中间一竖列是所有模块,和S相连,容量为A中的消耗,和T相连,容量为B中的消耗。对于额外的花费,只需要额外连接两点,正向弧和反向弧容量都为cost。
这样一来,恰好包括了所有的割。(想从A跨到B,必须经过额外消耗那条边)
试了一下,3000+ms,竟然过了。
代码如下:
/*
ID: j.sure.1
PROG:
LANG: C++
*/
/****************************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <climits>
#include <iostream>
#define LL long long
using namespace std;
const int INF = 0x3f3f3f3f;
/****************************************/
const int N = 22222, M = 4*N+4e5;
int n, m, tot, head[N], cur[N], lev[N], q[N], s[N], S, T;
struct Node {
	int u, v, cap;
	int next;
}edge[M];

void init()
{
	tot = 0; memset(head, -1, sizeof(head));
}

void add(int u, int v, int c)
{
	edge[tot].u = u; edge[tot].v = v; edge[tot].cap = c;
	edge[tot].next = head[u]; head[u] = tot++;
}

bool bfs()
{
	int fron = 0, rear = 0;
	memset(lev, -1, sizeof(lev));
	lev[S] = 0; q[rear++] = S;
	while(fron < rear) {
		int u = q[fron%N]; fron++;
		for(int i = head[u]; i != -1; i = edge[i].next) {
			int v = edge[i].v;
			if(edge[i].cap && lev[v] == -1) {
				lev[v] = lev[u] + 1;
				q[rear%N] = v; rear++;
				if(v == T) return true;
			}
		}
	}
	return false;
}

int Dinic()
{
	int ret = 0;
	while(bfs()) {
		memcpy(cur, head, sizeof(head));
		int u = S, top = 0;
		while(1) {
			if(u == T) {
				int mini = INF, loc;
				for(int i = 0; i < top; i++) {
					if(mini > edge[s[i]].cap) {
						mini = edge[s[i]].cap;
						loc = i;
					}
				}
				for(int i = 0; i < top; i++) {
					edge[s[i]].cap -= mini;
					edge[s[i]^1].cap += mini;
				}
				ret += mini;
				top = loc;
				u = edge[s[top]].u;
			}
			int &i = cur[u];
			for(; i != -1; i = edge[i].next) {
				int v = edge[i].v;
				if(edge[i].cap && lev[v] == lev[u] + 1) break;
			}
			if(i != -1) {
				s[top++] = i;
				u = edge[i].v;
			}
			else {
				if(!top) break;
				lev[u] = -1;
				u = edge[s[--top]].u;
			}
		}
	}
	return ret;
}

int main()
{
#ifdef J_Sure
//	freopen("000.in", "r", stdin);
//	freopen(".out", "w", stdout);
#endif
	scanf("%d%d", &n, &m);
	init();
	S = 0; T = n+1;
	int A, B;
	for(int i = 1; i <= n; i++) {
		scanf("%d%d", &A, &B);
		add(S, i, A); add(i, S, 0);
		add(i, T, B); add(T, i, 0);
	}
	int u, v, w;
	for(int i = 1; i <= m; i++) {
		scanf("%d%d%d", &u, &v, &w);
		add(u, v, w); add(v, u, w);
	}
	int ret = Dinic();
	printf("%d\n", ret);
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值