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() ;
}
}