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
Source
POJ Monthly--2007.11.25, Zhou Dong
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
最小割~
从S向所有任务连边表示A机器,从所有任务向T连边表示B机器,然后对于每一对x,y连双向边,表示如果选择不同就要花费权值,跑最小割即可~
#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
using namespace std;
int n,m,x,y,val,fi[20010],w[4001000],ne[4001000],v[4001000],dis[20010],cnt,ans;
void add(int u,int vv,int val)
{
w[++cnt]=vv;ne[cnt]=fi[u];fi[u]=cnt;v[cnt]=val;
w[++cnt]=u;ne[cnt]=fi[vv];fi[vv]=cnt;v[cnt]=0;
}
bool bfs()
{
queue<int> q;
memset(dis,-1,sizeof(dis));
q.push(0);dis[0]=0;
while(!q.empty())
{
int k=q.front();q.pop();
for(int i=fi[k];i;i=ne[i])
if(v[i]>0 && dis[w[i]]==-1)
{
dis[w[i]]=dis[k]+1;q.push(w[i]);
}
}
if(dis[n+1]==-1) return 0;
return 1;
}
int findd(int u,int vv)
{
if(u==n+1) return vv;
int kkz,tot=0;
for(int i=fi[u];i;i=ne[i])
if(v[i]>0 && dis[w[i]]==dis[u]+1 && (kkz=findd(w[i],min(v[i],vv-tot))))
{
v[i]-=kkz;v[i^1]+=kkz;tot+=kkz;
if(tot==vv) return tot;
}
if(!tot) dis[u]=-1;
return tot;
}
int main()
{
scanf("%d%d",&n,&m);cnt=1;
for(int i=1;i<=n;i++)
{
scanf("%d%d",&x,&y);
add(0,i,x);add(i,n+1,y);
}
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&val);
add(x,y,val);add(y,x,val);
}
while(bfs()) ans+=findd(0,999999999);
printf("%d\n",ans);
return 0;
}