Dual Core CPU

Dual Core CPU
Time Limit: 15000MS Memory Limit: 131072K
Total Submissions: 20935 Accepted: 9054
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: 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
将CPU1看成源点,将CPU2看成汇点,对于每个模块建立与源点和汇点容量弧,将不同模块a,b的额外花费建立容量为w的弧,建立起容量网络

#include <map>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <cstdio>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define LL long long
#define PI acos(-1.0)
#define MMM 0x3f3f3f3f
#define RR freopen("input.txt","r",stdin)
#define WW freopen("output.txt","w",stdout)

const int INF = 0x3f3f3f3f;

const int Max = 1000000;

struct Edge
{
    int v;
    int cap;
    int next;
}E[Max];

int top;

int n,m;

int Head[21000];

int Du[21000];

void Build(int u,int v,int w,int ww)
{
    E[top].v=v; E[top].cap=w;
    E[top].next=Head[u];
    Head[u]=top++;
    E[top].v=u; E[top].cap=ww;
    E[top].next=Head[v];
    Head[v]=top++;
}

bool bfs()
{
    memset(Du,0,sizeof(Du));
    Du[0]=1;
    queue<int>Q;
    Q.push(0);
    while(!Q.empty())
    {
        int a=Q.front();
        Q.pop();
        for(int i=Head[a];i!=-1;i=E[i].next)
        {
            if(Du[E[i].v]==0&&E[i].cap>0)
            {
                Du[E[i].v]=Du[a]+1;
                Q.push(E[i].v);
            }
        }
    }
    return Du[n+1];
}

int dfs(int star,int num)
{
    if(star==n+1||num==0)
    {
        return num;
    }
    int s=0;
    int ant;
    for(int i=Head[star];i!=-1;i =E[i].next)
    {
        if(Du[star]+1==Du[E[i].v]&&(ant=dfs(E[i].v,min(num,E[i].cap)))>0)
        {
            E[i].cap-=ant;
            num-=ant;
            s+=ant;
            E[i^1].cap+=ant;
            if(num==0)
            {
                break;
            }
        }
    }
    return s;
}

int Dinic()
{
    int ant=0;
    while(bfs())
    {
        ant+=dfs(0,INF);
    }
    return ant;
}

int main()
{
    int u,v,w,a,b;
    while(~scanf("%d %d",&n,&m))
    {
        top=0;
        memset(Head,-1,sizeof(Head));
        for(int i=1;i<=n;i++)
        {
            scanf("%d %d",&a,&b);
            Build(0,i,a,0);
            Build(i,n+1,b,0);
        }
        for(int i=1;i<=m;i++)
        {
            scanf("%d %d %d",&u,&v,&w);
            Build(u,v,w,w);
        }
        printf("%d
",Dinic());
    }
    return 0;
}
原文地址:https://www.cnblogs.com/juechen/p/5256014.html