Dual Core CPU POJ - 3469 经典最小割

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

 最小费用将对象划分成两个集合的问题,常常可以转换为最小割顺利解决。

这是一个经典的最小割的问题,考虑把那个模块在那个核上执行分成两个集合。



#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<queue>
#include<vector>
#include<map>
#include<set>
#include<stack>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int M = 4e5 + 10;
int n,m;
struct node
{
    int to,cap,rev;
};
vector<node>e[M];//图的邻接表表示
int level[M];//顶点到源点的距离标号
int iter[M];//当前弧,在其之前的边没用
//增加一条从l到r容量为c的边
void init(int l,int r,int c)
{
    e[l].push_back((node){r,c,e[r].size()});
    e[r].push_back((node){l,0,e[l].size()-1});
}
//通过bfs计算从源点出发的距离标号
void bfs(int s)
{
    memset(level,-1,sizeof(level));
    queue<int>q;
    level[s]=0;
    q.push(s);
    while(!q.empty())
    {
        int v=q.front();
        q.pop();
        for(int i=0;i<e[v].size();i++)
        {
            node &ee=e[v][i];
           // cout<<ee.to<<" "<<ee.cap<<" "<<level[ee.to]<<" "<<level[v]+1<<endl;
            if(ee.cap>0&&level[ee.to]<0)
            {
                level[ee.to]=level[v]+1;
                q.push(ee.to);
            }
        }
    }
}
//通过dfs寻找增广路
int dfs(int v,int t,int f)
{
    if(v==t)
        return f;
    for(int &i=iter[v];i<e[v].size();i++)
    {
        node &ee=e[v][i];
        if(ee.cap>0&&level[v]<level[ee.to])
        {
            int d=dfs(ee.to,t,min(f,ee.cap));
            if(d>0)
            {
                //cout<<d<<"*"<<endl;
                ee.cap-=d;
                e[ee.to][ee.rev].cap+=d;
                return d;
            }
        }
    }
    return 0;
}
int max_flow(int s,int t)
{
    int ff=0;
    for(;;)
    {
        bfs(s);

        if(level[t]<0)
            return ff;
        //cout<<s<<" "<<t<<endl;
        memset(iter,0,sizeof(iter));
        int f;
        while((f=dfs(s,t,inf))>0){
            ff+=f;
        }
    }
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        int s=0,t=n+1;//s集合为A,t集合为B;及从源点到汇点的最小割
        int a,b,w;
        memset(e,0,sizeof(e));
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&a,&b);
            init(s,i,a);
            init(i,t,b);
        }
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d",&a,&b,&w);
            init(a,b,w);
            init(b,a,w);
        }
        int ans=max_flow(s,t);
        printf("%d\n",ans);
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/liluoyu_1016/article/details/80149424
今日推荐