Dual Core CPU
Time Limit: 15000MS Memory Limit: 131072K
Total Submissions: 20038 Accepted: 8696
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: 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
Output only one integer, the minimum total cost.
Sample Input
3 1
1 10
2 10
10 3
2 3 1000
Sample Output
13
大概意思:
- 有n个任务,可以由a机器或b机器完成,花费分别为ai,bi,现有m个二元关系(x , y) ,x任务和y任务若不在相同的机器上完成就会增加花费v,求完成所有任务的最小花费
解法:
- source向i任务建一条a机器的花费,i向sink建一条b机器的花费,对于每个二元关系,建一容量为v的无向边,求最小割
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cassert>
using namespace std ;
const int INF = 1<<27 ;
const int N = 2e4 + 11 ;
const int M = 2e6 + 11 ;
int head[N] , flow[M] , to[M] , info[M] ; int nedge ;
int dis[N] , gap[N] ; int gnode ;
int source , sink ;
int n , m ;
void add_edge(int a , int b , int c) {
if(head[a] == -1) ++gnode ;
if(head[b] == -1) ++gnode ;
to[++nedge] = b ;
flow[nedge] = c ;
info[nedge] = head[a] ;
head[a] = nedge ;
swap(a , b) ;
to[++nedge] = b ;
flow[nedge] = 0 ;
info[nedge] = head[a] ;
head[a] = nedge ;
}
void init() {
int a , b , c;
source = 0 , sink = n+1 ;
fill(head , head+N , -1) ; nedge = -1 ; gnode = 0 ;
for(int i = 1 ; i <= n ; ++i) {
scanf("%d%d" ,&a ,&b) ;
add_edge(source , i , a) ;
add_edge(i , sink , b) ;
}
while(m--) {
scanf("%d%d%d" ,&a ,&b , &c) ;
add_edge(a , b , c) ;
add_edge(b , a , c) ;
}
}
int dfs(int u , int limit) {
if(u == sink) return limit ;
int f1 = 0 , f ;
int minh = gnode-1 ;
for(int i = head[u] ; i != -1 ; i = info[i] ) {
int v = to[i] ;
if(flow[i] > 0) {
if(dis[v]+1 == dis[u]) {
f = dfs(v , min(limit , flow[i])) ;
flow[i] -= f ;
flow[i^1] += f ;
limit -= f ;
f1 += f ;
if(dis[source] >= gnode) return f1 ;
if(limit == 0) break ;
}
minh = min(minh , dis[v]) ;
}
}
if(f1 == 0) {
--gap[dis[u]] ;
if(gap[dis[u]] == 0) dis[source] = gnode ;
dis[u] = minh + 1 ;
++gap[dis[u]] ;
}
return f1 ;
}
void sap() {
fill(dis , dis+N , 0) ;
fill(gap , gap+N , 0) ;
gap[source] = gnode ;
int all = 0 ;
while(dis[source] < gnode) {
all += dfs(source , INF) ;
}
printf("%d\n" , all) ;
}
int main() {//freopen("data.in" , "r" , stdin) ;
while(scanf("%d%d" , &n ,&m)==2) {
init() ;
sap() ;
}
}