问题描述
总时间限制:
15000ms
单个测试点时间限制:
5000ms
内存限制:
65536kB
描述
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.
输入
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 only one integer, the minimum total cost.
样例输入
3 1 1 10 2 10 10 3 2 3 1000
样例输出
13
问题解决
点在中间,s表示第一个cpu,t表示第二个cpu,求最小割转化为最大流
/*
* Dinic algo for max flow
*
* This implementation assumes that #nodes, #edges, and capacity on each edge <= INT_MAX,
* which means INT_MAX is the best approximation of INF on edge capacity.
* The total amount of max flow computed can be up to LLONG_MAX (not defined in this file),
* but each 'dfs' call in 'dinic' can return <= INT_MAX flow value.
*/
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <assert.h>
#include <queue>
#include <vector>
#include <algorithm>
#define N (20000+100)
#define M (2000000)
typedef long long LL;
using namespace std;
struct edge {
int v, cap, next;
};
edge e[M];
int head[N], level[N], cur[N];
int num_of_edges;
/*
* When there are multiple test sets, you need to re-initialize before each
*/
void dinic_init(void) {
num_of_edges = 0;
memset(head, -1, sizeof(head));
return;
}
int add_edge(int u, int v, int c1, int c2) {
int& i=num_of_edges;
assert(c1>=0 && c2>=0 && c1+c2>=0); // check for possibility of overflow
e[i].v = v;
e[i].cap = c1;
e[i].next = head[u];
head[u] = i++;
e[i].v = u;
e[i].cap = c2;
e[i].next = head[v];
head[v] = i++;
return i;
}
void print_graph(int n) {
for (int u=0; u<n; u++) {
printf("%d: ", u);
for (int i=head[u]; i>=0; i=e[i].next) {
printf("%d(%d)", e[i].v, e[i].cap);
}
printf("\n");
}
return;
}
/*
* Find all augmentation paths in the current level graph
* This is the recursive version
*/
int dfs(int u, int t, int bn) {
if (u == t) return bn;
int left = bn;
for (int &i=cur[u]; i>=0; i=e[i].next) {
int v = e[i].v;
int c = e[i].cap;
if (c > 0 && level[u]+1 == level[v]) {
int flow = dfs(v, t, min(left, c));
if (flow > 0) {
e[i].cap -= flow;
e[i^1].cap += flow;
cur[u] = i;
left -= flow;
if (!left) break;
}
}
}
if (left > 0) level[u] = 0;
return bn - left;
}
bool bfs(int s, int t) {
memset(level, 0, sizeof(level));
level[s] = 1;
queue<int> q;
q.push(s);
while (!q.empty()) {
int u = q.front();
q.pop();
if (u == t) return true;
for (int i=head[u]; i>=0; i=e[i].next) {
int v = e[i].v;
if (!level[v] && e[i].cap > 0) {
level[v] = level[u]+1;
q.push(v);
}
}
}
return false;
}
LL dinic(int s, int t) {
LL max_flow = 0;
while (bfs(s, t)) {
memcpy(cur, head, sizeof(head));
max_flow += dfs(s, t, INT_MAX);
}
return max_flow;
}
int upstream(int s, int n) {
int cnt = 0;
vector<bool> visited(n);
queue<int> q;
visited[s] = true;
q.push(s);
while (!q.empty()) {
int u = q.front();
q.pop();
for (int i=head[u]; i>=0; i=e[i].next) {
int v = e[i].v;
if (e[i].cap > 0 && !visited[v]) {
visited[v] = true;
q.push(v);
cnt++;
}
}
}
return cnt; // excluding s
}
int main(){
int n,m;
while(~scanf("%d%d",&n,&m)){
dinic_init();
int s=0,t=n+1;
for(int i=1;i<=n;i++){
int l,r;
scanf("%d%d",&l,&r);
add_edge(s,i,l,0);
add_edge(i,t,r,0);
}
for(int i=1;i<=m;i++){
int from,to,val;
scanf("%d%d%d",&from,&to,&val);
add_edge(from,to,val,0);
add_edge(to,from,val,0);
}
printf("%lld\n",dinic(s,t));
}
return 0;
}