poj3469 Dual Core CPU

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

题目大意:一个cpu有两个核,有n个任务,m个要求,第i个任务如果用第一个核处理需要花费ai,第二个需要花费bi,对于m个要求:如果任务i和j不在同一个核上运行,则要额外花费c,求最小花费.
分析:感觉又像是匹配问题,关系比较复杂,不能用二分图来做,于是就用网络流.
          如果考虑流的意义,图比较难建,考虑最小割的意义.删掉权值和最小的边使得源点汇点不连通.为了保证每个任务只会选一个核,将A核作为源点,B核作为汇点,源点向每个任务连一条容量为A花费的边,每个任务向汇点连一条容量为B花费的边,这样如果没有m个要求的话,最小割=答案便成立.如果有要求a,b,c.则在a,b之间互相连权值为c的边.这样如果a,b用的不是同一核,删掉两条边后会因为新加的这条边而继续相连.所以最小割=答案. 
          最大流问题建模考虑答案与最大流或最小割之间的关系.
#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int maxn = 5000010,inf = 0x7fffffff;

int n,f,d,S,T,head[maxn],to[maxn],nextt[maxn],tot = 2,w[maxn],ans,m;
int vis[maxn];

void add(int x,int y,int z)
{
    w[tot] = z;
    to[tot] = y;
    nextt[tot] = head[x];
    head[x] = tot++;

    w[tot] = 0;
    to[tot] = x;
    nextt[tot] = head[y];
    head[y] = tot++;
}

bool bfs()
{
    queue <int> q;
    memset(vis,-1,sizeof(vis));
    vis[S] = 0;
    q.push(S);
    while (!q.empty())
    {
        int u = q.front();
        q.pop();
        if (u == T)
            return true;
        for (int i = head[u];i;i = nextt[i])
        {
            int v = to[i];
            if (vis[v] == -1 && w[i])
            {
                vis[v] = vis[u] + 1;
                q.push(v);
            }
        }
    }
    return false;
}

int dfs(int u,int f)
{
    int res = 0;
    if (u == T)
        return f;
    for (int i = head[u];i;i = nextt[i])
    {
        int v = to[i];
        if (w[i] && vis[v] == vis[u] + 1)
        {
            int temp = dfs(v,min(f - res,w[i]));
            res += temp;
            w[i] -= temp;
            w[i ^ 1] += temp;
            if (res == f)
                return res;
        }
    }
    if (!res)
        vis[u] = -1;
    return res;
}

void dinic()
{
    while (bfs())
       ans += dfs(S,inf);
}

int main()
{
    scanf("%d%d",&n,&m);
    S = 0;
    T = n + 1;
    for (int i = 1; i <= n; i++)
    {
        int a,b;
        scanf("%d%d",&a,&b);
        add(S,i,a);
        add(i,T,b);
    }
    for (int i = 1; i <= m; i++)
    {
        int a,b,c;
        scanf("%d%d%d",&a,&b,&c);
        add(a,b,c);
        add(b,a,c);
    }
    dinic();
    printf("%d\n",ans);

    return 0;
}

 

posted @ 2017-12-28 21:54  zbtrs  阅读(320)  评论(0编辑  收藏  举报