POJ3469 Dual Core CPU

Dual Core CPU
Time Limit: 15000MS Memory Limit: 131072K
Total Submissions: 24740 Accepted: 10705
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



-——————————————————————————————————

题目的意思是给你n个模块,每个模块在A核花费为ai,在B核跑花费为bi,然后由m个任务(ai,bi,wi),表示如果ai,bi不在同一个核上跑,额外的花费为wi,求最小的花费。

思路:求最小割,建图:源点指每一给点,容量为在A上所花的代价,反向边容量为0,然后每个点指向汇点,容量为在B上所花的代价,反向边容量为0,然后多花费代价c,就让a和b之间连一条边,来回的容量都为c

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>

using namespace std;

#define LL long long
const int INF = 0x3f3f3f3f;
#define MAXN 200005

struct node
{
    int u, v, next, cap;
} edge[4000006];
int nt[MAXN], s[MAXN], d[MAXN], visit[MAXN];
int cnt;

void init()
{
    cnt = 0;
    memset(s, -1, sizeof(s));
}

void add(int u, int v, int c,int c2)
{
    edge[cnt].u = u;
    edge[cnt].v = v;
    edge[cnt].cap = c;
    edge[cnt].next = s[u];
    s[u] = cnt++;
    edge[cnt].u = v;
    edge[cnt].v = u;
    edge[cnt].cap = c2;
    edge[cnt].next = s[v];
    s[v] = cnt++;
}

bool BFS(int ss, int ee)
{
    memset(d, 0, sizeof d);
    d[ss] = 1;
    queue<int>q;
    q.push(ss);
    while (!q.empty())
    {
        int pre = q.front();
        q.pop();
        for (int i = s[pre]; ~i; i = edge[i].next)
        {
            int v = edge[i].v;
            if (edge[i].cap > 0 && !d[v])
            {
                d[v] = d[pre] + 1;
                q.push(v);
            }
        }
    }
    return d[ee];
}

int DFS(int x, int exp, int ee)
{
    if (x == ee||!exp) return exp;
    int temp,flow=0;
    for (int i = nt[x]; ~i ; i = edge[i].next, nt[x] = i)
    {
        int v = edge[i].v;
        if (d[v] == d[x] + 1&&(temp = (DFS(v, min(exp, edge[i].cap), ee))) > 0)
        {
            edge[i].cap -= temp;
            edge[i ^ 1].cap += temp;
            flow += temp;
            exp -= temp;
            if (!exp) break;
        }
    }
    if (!flow) d[x] = 0;
    return flow;
}

int Dinic_flow(int ss,int ee)
{


    int ans = 0;
    while (BFS(ss, ee))
    {
        for (int i = 0; i <=ee; i++) nt[i] = s[i];
        ans+= DFS(ss, INF, ee);
    }
    return ans;
}


int main()
{
    int n,m,u,v,c;
    while(~scanf("%d%d",&n,&m))
    {
        init();
        for(int i=1; i<=n; i++)
            scanf("%d%d",&u,&v),add(0,i,u,0),add(i,n+1,v,0);

        for(int i=0; i<m; i++)
        {
            scanf("%d%d%d",&u,&v,&c);
            add(u,v,c,c);
        }
        printf("%d\n",Dinic_flow(0,n+1));
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值