POJ 3469.Dual Core CPU 最大流dinic算法模板

时间:2021-02-25 05:39:12
Time Limit: 15000MS   Memory Limit: 131072K
Total Submissions: 24830   Accepted: 10756
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

Source

 
题意:每个问题在a,b处理器上面处理有不同的代价,有一些问题在不同的处理器上面处理要增加额外的代价,求处理完所有问题需要的最小代价。
思路:将问题分别于a,b相连,边权为代价。将它们分成两个集合,与a在一个集合的表示在a上面处理,与b在一个集合的表示在b上面处理,如果要代价最小的话,那就是最小割。但是有一些物品在不同的处理器上面处理需要额外的代价,,那就在它们之间建立边权,这样的话将它们分开就会增加代价。最小割问题即求最大流。这题的边权值较大,注意如果用一般增广路算法(即在残留网络中,每次任意寻找一条增广路)的话时间复杂度为O(FVE),与最大流有关,最短增广路算法(在层次网络中,用dfs实现多次增广,即dinic算法)的时间复杂度为O(VVE)。
代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
#define PI acos(-1.0)
typedef long long ll;
typedef pair<int,int> P;
const int maxn=1e5+,maxm=1e5+,inf=0x3f3f3f3f,mod=1e9+;
const ll INF=1e18+;
priority_queue<P,vector<P>,greater<P> >que;
struct edge
{
int from,to;
int cap;
int rev;///方向边的编号
};
vector<edge>G[maxn];///邻接表存图
int iter[maxn];///当前弧,在其之前的边已经没有了作用
int level[maxn];///层次
void addedge(int u,int v,int c)
{
edge e;
e.from=u,e.to=v,e.cap=c,e.rev=G[v].size();
G[u].push_back(e);
e.from=v,e.to=u,e.cap=,e.rev=G[u].size()-;
G[v].push_back(e);
}
int bfs(int s,int t)
{
memset(level,-,sizeof(level));
queue<int>q;
level[s]=;
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=; i<G[u].size(); i++)
{
edge e=G[u][i];
if(e.cap>&&level[e.to]<)
{
level[e.to]=level[u]+;
q.push(e.to);
}
}
}
if(level[t]<) return ;
else return ;
}
int dfs(int u,int t,int f)
{
if(u==t||f==) return f;
int flow=;
for(int &i=iter[u]; i<G[u].size(); i++)
{
edge e=G[u][i];
if(e.cap>&&level[u]+==level[e.to])
{
int d=dfs(e.to,t,min(f,e.cap));
if(d>)
{
G[u][i].cap-=d;
G[e.to][e.rev].cap+=d;
flow+=d;
f-=d;
if(f==) break;
}
}
}
return flow;
}
int max_flow(int s,int t)
{
int flow=;
while(bfs(s,t))
{
memset(iter,,sizeof(iter));
flow+=dfs(s,t,inf);
}
return flow;
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
int s=,t=n+;
for(int i=; i<=n; i++)
{
int a,b;
scanf("%d%d",&a,&b);
addedge(s,i,a);
addedge(i,t,b);
}
for(int i=; i<=m; i++)
{
int a,b,w;
scanf("%d%d%d",&a,&b,&w);
addedge(a,b,w);
addedge(b,a,w);
}
cout<<max_flow(s,t)<<endl;
return ;
}

最大流模板