# POJ 3469-Dual Core CPU(网络流_最小割求容量)

Dual Core CPU
 Time Limit: 15000MS Memory Limit: 131072K Total Submissions: 19724 Accepted: 8562 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

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <queue>

using namespace std;
const int inf=0x3f3f3f3f;
int n,cnt,s,t,nv;

struct node {
int u, v, cap, next;
} edge[1000000];

void add(int u, int v, int cap)
{
edge[cnt].v=v;
edge[cnt].cap=cap;

edge[cnt].v=u;
edge[cnt].cap=0;
}

void bfs()
{
memset(d,-1,sizeof(d));
memset(num,0,sizeof(num));
queue<int >q;
q.push(t);
d[t]=0;
num[0]=1;
while(!q.empty()) {
int i;
int u=q.front();
q.pop();
int v=edge[i].v;
if(d[v]==-1) continue;
d[v]=d[u]+1;
num[d[v]]++;
q.push(v);
}
}
}

void isap()
{
int flow=0, u=pre[s]=s, i;
bfs();
while(d[s]<nv) {
if(u==t) {
int f=inf, pos;
for(i=s; i!=t; i=edge[cur[i]].v) {
if(f>edge[cur[i]].cap) {
f=edge[cur[i]].cap;
pos=i;
}
}
for(i=s; i!=t; i=edge[cur[i]].v) {
edge[cur[i]].cap-=f;
edge[cur[i]^1].cap+=f;
}
flow+=f;
u=pos;
}
for(i=cur[u]; i!=-1; i=edge[i].next) {
if(d[edge[i].v]+1==d[u]&&edge[i].cap)
break;
}
if(i!=-1) {
cur[u]=i;
pre[edge[i].v]=u;
u=edge[i].v;
} else {
if(--num[d[u]]==0) break;
int mind=nv;
if(mind>d[edge[i].v]&&edge[i].cap) {
mind=d[edge[i].v];
cur[u]=i;
}
}
d[u]=mind+1;
num[d[u]]++;
u=pre[u];
}
}
printf("%d\n",flow);
}
int main()
{
int m,i;
int a,b,c;
scanf("%d %d",&n,&m);
s=0;
t=n+1;
nv=t+1;
cnt=0;
for(i=1;i<=n;i++){
scanf("%d %d",&a,&b);
}
while(m--){
scanf("%d %d %d",&a,&b,&c);
}
isap();
return 0;
}


©️2019 CSDN 皮肤主题: 酷酷鲨 设计师: CSDN官方博客