您的位置:首页 > 其它

POJ 3469 --Dual Core CPU【最小割】

2015-08-07 14:07 288 查看
Dual Core CPU

Time Limit: 15000MSMemory Limit: 131072K
Total Submissions: 20852Accepted: 9010
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


题目描述:

由于越来越多的计算机配置了双核CPU,TinySoft公司的首席技术官员,SetagLilb,决定升级他们的产品-SWODNIW。SWODNIW包含了N个模块,每个模块必须运行在某个CPU中。每个模块在每个CPU中运行的耗费已经被估算出来了,设为Ai和Bi。同时,M对模块之间需要共享数据,如果他们运行在同一个CPU中,共享数据的耗费可以忽略不计,否则,还需要额外的费用。你必须很好地安排这N个模块,使得总耗费最小。

思路: 如果将两个CPU分别视为源点和汇点、模块视为顶点,则可以按照以下方式构图:对于第i个模块在每个CPU中的耗费Ai和Bi, 从源点向顶点i连接一条容量为Ai的弧、从顶点i向汇点连接一条容量为Bi的弧;对于a模块与b模块在不同CPU中运行造成的额外耗费w,顶点a与顶点b连接一条容量为w的弧,顶点b与顶点a连接一条容量为w的弧。此时每个顶点(模块)都和源点及汇点(两个CPU)相连,即每个模块都可以在任意一个CPU中运行。

对于图中的任意一个割,源点与汇点必不连通。因此每个顶点(模块)都不可能同时和源点及汇点(两个CPU)相连,即每个模块只在同一个CPU中运行。此时耗费即为割的容量。很显然,当割的容量取得最小值时,总耗费最小。故题目转化为求最小割的容量。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define maxn 21000
#define maxm 1000000
#define INF 0x3f3f3f3f
using namespace std;
int n, m;
int head[maxn],cur[maxn], cnt;
int dist[maxn], vis[maxn];
struct node{
    int u, v, cap, flow, next;
};
node edge[maxm];

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

void add(int u, int v, int w){
    node E1 = {u, v, w, 0, head[u]};
    edge[cnt] = E1;
    head[u] = cnt++;
    node E2 = {v, u, 0, 0, head[v]};
    edge[cnt] = E2;
    head[v] = cnt++;
}

void getmap(){
    int a, b, w;
    for(int i = 1; i <= n; ++i){
        scanf("%d%d", &a, &b);
        add(0, i, a);
        add(i, n + 1, b);
    }
    for(int j = 1; j <=m; ++j){
        scanf("%d%d%d", &a, &b, &w);
        add(a, b, w);
        add(b, a, w);
    }
}

bool BFS(int st, int ed){
    queue<int>q;
    memset(vis, 0 ,sizeof(vis));
    memset(dist, -1, sizeof(dist));
    vis[st] = 1;
    dist[st] = 0;
    q.push(st);
    while(!q.empty()){
        int u = q.front();
        q.pop();
        for(int i = head[u]; i != -1; i = edge[i].next){
            node E = edge[i];
            if(!vis[E.v] && E.cap > E.flow){
                vis[E.v] = 1;
                dist[E.v] = dist[u] + 1;
                if(E.v == ed) return true;
                q.push(E.v);
            }
        }
    }
    return false;
}

int DFS(int x, int ed, int a){
    if(a == 0 || x == ed)
        return a;
    int flow = 0, f;
    for(int &i = cur[x]; i != -1; i = edge[i].next){
        node &E = edge[i];
        if(dist[E.v] == dist[x] + 1 && (f = DFS(E.v, ed, min(a, E.cap - E.flow))) > 0){
            E.flow += f;
            edge[i ^ 1].flow -= f;
            flow += f;
            a -= f;
            if(a == 0) break;
        }
    }
    return flow;
}

int maxflow(int st, int ed){
    int sumflow = 0;
    while(BFS(st,ed)){
        memcpy(cur, head, sizeof(head));
        sumflow += DFS(st, ed, INF);
    }
    return sumflow;
}

int main (){
    while(scanf("%d%d", &n,&m) != EOF){
        init();
        getmap();
        printf("%d\n", maxflow(0, n + 1));
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: