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
题目大意
要在由核A和核B组成的双核CPU上运行N个模块。模块i在核A上执行的花费为Ai,在核B上执行的花费为Bi。有M个相互之间需要进行数据交换的模块组合(ai,bi),如果这两个模块在同一个核上执行则没有额外花费,否则会产生wi的花费。请计算执行所有模块所需的最小花费。
解题思路
用最小的费用将对象划分成两个集合的问题,常常可以转换成最小割问题。本题考虑把N个模块按照在哪个核上执行分成两个集合。
记在核A上执行的模块集合为S,而在核B上执行的模块集合为T。考虑以模块为顶点,并且还有额外的源点s和汇点t的图。我们也记图的s-t割所对应的包含s的顶点集合为S,包含t的集合为T,然后来考察他们的对应关系。此时,花费的总和是
如果我们可以通过合适的建边使得花费的总和等价于割的容量的话,那么求最小花费只要求最小割即可。
具体建图方法详见挑战P237
注:该题数据规模非常大,最好使用Dinic等比较快速的网络流算法。
代码如下:
#include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
struct edge{int to,cap,rev;};
vector<edge> G[20005];
int used[20005];
int level[20005];
int iter[20005];
int N,M;
int A[20005],B[20005];
int a[200005],b[200005],w[200005];
void add_edge(int from,int to,int cap)
{
edge temp;
temp.to=to;
temp.cap=cap;
temp.rev=G[to].size();
G[from].push_back(temp);
temp.to=from;
temp.cap=0;
temp.rev=G[from].size()-1;
G[to].push_back(temp);
}
void bfs(int s)
{
memset(level,-1,sizeof(level));
queue<int> que;
level[s]=0;
que.push(s);
while(!que.empty())
{
int v=que.front();
que.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;
que.push(e.to);
}
}
}
}
int dfs(int v,int t,int f)
{
if(v==t)
return f;
for(int &i=iter[v];i<G[v].size();i++)
{
edge &e=G[v][i];
if(e.cap>0&&level[v]<level[e.to])
{
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 flow=0;
for(;;)
{
bfs(s);
if(level[t]<0)
return flow;
memset(iter,0,sizeof(iter));
int f;
while((f=dfs(s,t,10000000))>0)
{
flow+=f;
}
}
}
void solve()
{
int s=N,t=s+1;
for(int i=0;i<N;i++)
{
add_edge(i,t,A[i]);
add_edge(s,i,B[i]);
}
for(int i=0;i<M;i++)
{
add_edge(a[i]-1,b[i]-1,w[i]);
add_edge(b[i]-1,a[i]-1,w[i]);
}
cout<<max_flow(s,t)<<endl;
}
int main()
{
int i,j;
while(cin>>N>>M)
{
for(i=0;i<N;i++)
{
cin>>A[i]>>B[i];
}
for(i=0;i<M;i++)
{
cin>>a[i]>>b[i]>>w[i];
}
solve();
}
return 0;
}