poj 3469 Dual Core CPU 建图方法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wuxiaowu547/article/details/81952303

题目:

Dual Core CPU
Time Limit: 15000MS Memory Limit: 131072K
Total Submissions: 26611 Accepted: 11484
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
题意:
有两个CPU,现在N个工作要进行,两个CPU对于不同的工作都有不同的花费,有一些特殊的要求:有些工作如果不在同一个CPU进行,那么就要花费额外的代价。问你完成这些工作最少需要多少钱。
思路:
最小割。源点向目标点连边,权值为花费,目标点向汇点连边,权值为花费,然后目标点向目标点连双向边,表示两个工作如果不在一起将要耗费的花费。然后就是最小割,其实这样建图的话,最小割的意义就体现出来了,花费最小的代价来使得工作形成两个互不相交的集合。
建图:

这里写图片描述

代码:

#include <iostream>
#include <string>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <vector>
#include <queue>
using namespace std;

const int N = 20100;
const int INF = 0x3f3f3f3f;
struct edge
{
    int to, cap, next;
}g[N*100];
int level[N], iter[N], head[N], que[N];
int n, m, cnt;

void add_edge(int v, int u, int cap)
{
    g[cnt].to = u, g[cnt].cap = cap, g[cnt].next = head[v], head[v] = cnt++;
    g[cnt].to = v, g[cnt].cap = 0, g[cnt].next = head[u], head[u] = cnt++;
}
bool bfs(int s, int t)
{
    memset(level, -1, sizeof level);
    level[s] = 0;
    int st = 0, en = 0;
    que[0] = s;
    while(st <= en)
    {
        int v = que[st++];
        for(int i = head[v]; i != -1; i = g[i].next)
        {
            int u = g[i].to;
            if(g[i].cap > 0 && level[u] < 0)
            {
                level[u] = level[v] + 1;
                que[++en] = u;
            }
        }
    }
    return level[t] == -1;
}
//下面两个dfs都是当前弧+多路增广
int dfs(int v, int t, int f)
{
    if(v == t) return f;
    int num = 0;
    for(int &i = iter[v]; i != -1; i = g[i].next)
    {
        int u = g[i].to;
        if(g[i].cap > 0 && level[v] < level[u])
        {
            int d = dfs(u, t, min(f - num, g[i].cap));
            g[i].cap -= d, g[i^1].cap += d, num += d;
            if(num >= f) break;
        }
    }
    if(! num) level[v] = -1;
    return num;
}
int dfs(int v, int t, int f)
{
    if(v == t) return f;
    int tm = f;
    for(int &i = iter[v]; i != -1; i = g[i].next)
    {
        int u = g[i].to;
        if(g[i].cap > 0 && level[v] < level[u])
        {
            int d = dfs(u, t, min(tm, g[i].cap));
            g[i].cap -= d, g[i^1].cap += d, tm -= d;
            if(tm == 0) break;
        }
    }
    if(tm == f) level[v] = -1;
    return f - tm;
}
int dinic(int s, int t)
{
    int flow = 0, f;
    while(true)
    {
        if(bfs(s, t)) return flow;
        memcpy(iter, head, sizeof head);
        flow += dfs(s, t, INF);
    }
}
int main()
{
    int a, b, c;
    while(~ scanf("%d%d", &n, &m))
    {
        cnt = 0;
        memset(head, -1, sizeof head);
        for(int i = 1; i <= n; i++)
        {
            scanf("%d%d", &a, &b);
            add_edge(0, i, a);
            add_edge(i, n + 1, b);
        }
        for(int i = 0; i < m; i++)
        {
            scanf("%d%d%d", &a, &b, &c);
            add_edge(a, b, c);
            add_edge(b, a, c);
        }
        printf("%d\n", dinic(0, n + 1));
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/wuxiaowu547/article/details/81952303