您的位置:首页 > 其它

POJ 3469 Dual Core CPU(巧妙转化最大流)

2017-09-27 13:34 609 查看
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

题意:有一个双核的cpu,有N个任务,每个任务在第一个核心上会花费Ai,在第二个核心上会花费Bi,另外有M对任务是相互协作的,如果每对任务不在一个核心上工作会产生一个额外的花费,求完成N个任务的最小花费

把两个核心当做源点与汇点,然后如下建图:



对于w就是直接链接二者,然后求一下图的最小割(即最大流)就可以了。

代码如下:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<vector>
#include<set>
#include<algorithm>
#include<map>
#include<queue>
using namespace std;
const int MAX_V = 20010;
const int INF = 0x3f3f3f3f;
int V,E;
struct Edge{
int to,cap,rev;
};
vector<Edge> G[MAX_V];
int level[MAX_V],iter[MAX_V];
void add(int u,int v,int cap){
G[u].push_back((Edge){v,cap,(int)G[v].size()});
G[v].push_back((Edge){u,0,(int)G[u].size()-1});
}
void bfs(int s_) {
memset(level,-1,sizeof(level));
level[s_] = 0;
queue<int> q;
q.push(s_);
while(!q.empty()){
int v = q.front();q.pop();
for(int i=0;i<G[v].size();++i){
Edge &e = G[v][i];
if(e.cap > 0 && level[e.to] < 0) {
level[e.to] = level[v] + 1;
q.push(e.to);
}
}
}
}

int dfs(int s,int t,int f){
if(s == t)  return f;
for(int &i = iter[s];i<G[s].size();++i){
Edge &e = G[s][i];
if(e.cap > 0 && level[e.to] > level[s]){
int d = dfs(e.to,t,min(f,e.cap));
if(d > 0){
e.cap -= d;
G[e.to][e.rev].cap += d;
return d;
}
}
}
return 0;
}

int max_flow(int s,int t){
int res = 0;
while(true){
bfs(s);
if(level[t] < 0)    return res;
memset(iter,0,sizeof(iter));
int f = 0;
while((f = dfs(s,t,INF)) > 0)
res += f;
}
}
int main(void){
int s,t;
scanf("%d %d",&V,&E);
s = 0,t = V+1;
int a,b,c;
for(int i=1;i<=V;i++){
scanf("%d %d",&a,&b);
add(s,i,a);add(i,t,b);
}
for(int i=1;i<=E;i++){
scanf("%d %d %d",&a,&b,&c);
add(a,b,c);add(b,a,c);
}
printf("%d\n",max_flow(s,t));

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: