码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 3469-Dual Core CPU(网络流_最小割求容量)

时间:2015-01-26 12:00:53      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:

PS:最小割第一发,我怎么感觉就是求最大流呢,sad.这是求最小割的容量的问题,根据最大流最小割定理,可以转换成求解网络流的最大流流量。这个事==最大流的。


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

题意:双核CPU-SWODNIW 包含了N个模块,每个模块必须运行在某个CPU里面。每个模块在每个CPU的运行耗费已经被估算出来了,就是a,b。同时M对模块之间需要数据共享,如果他们不运行在一个CPU里面,会产生额外消费c.所以必须很好的安排N个模块,使总消耗最小。

思路:将两个CPU视为源点和汇点,模块视为顶点,对于a,b不在一个模块中产生的消耗,在a,b直接连接一条无向边。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <queue>

using namespace std;
const int inf=0x3f3f3f3f;
int head[30010],num[30010],d[30010],pre[30010],cur[30010];
int n,cnt,s,t,nv;

struct node {
    int u, v, cap, next;
} edge[1000000];

void add(int u, int v, int cap)
{
    edge[cnt].v=v;
    edge[cnt].cap=cap;
    edge[cnt].next=head[u];
    head[u]=cnt++;

    edge[cnt].v=u;
    edge[cnt].cap=0;
    edge[cnt].next=head[v];
    head[v]=cnt++;
}

void bfs()
{
    memset(d,-1,sizeof(d));
    memset(num,0,sizeof(num));
    queue<int >q;
    q.push(t);
    d[t]=0;
    num[0]=1;
    while(!q.empty()) {
        int i;
        int u=q.front();
        q.pop();
        for(i=head[u]; i!=-1; i=edge[i].next) {
            int v=edge[i].v;
            if(d[v]==-1) continue;
            d[v]=d[u]+1;
            num[d[v]]++;
            q.push(v);
        }
    }
}

void isap()
{
    memcpy(cur,head,sizeof(cur));
    int flow=0, u=pre[s]=s, i;
    bfs();
    while(d[s]<nv) {
        if(u==t) {
            int f=inf, pos;
            for(i=s; i!=t; i=edge[cur[i]].v) {
                if(f>edge[cur[i]].cap) {
                    f=edge[cur[i]].cap;
                    pos=i;
                }
            }
            for(i=s; i!=t; i=edge[cur[i]].v) {
                edge[cur[i]].cap-=f;
                edge[cur[i]^1].cap+=f;
            }
            flow+=f;
            u=pos;
        }
        for(i=cur[u]; i!=-1; i=edge[i].next) {
            if(d[edge[i].v]+1==d[u]&&edge[i].cap)
                break;
        }
        if(i!=-1) {
            cur[u]=i;
            pre[edge[i].v]=u;
            u=edge[i].v;
        } else {
            if(--num[d[u]]==0) break;
            int mind=nv;
            for(i=head[u]; i!=-1; i=edge[i].next) {
                if(mind>d[edge[i].v]&&edge[i].cap) {
                    mind=d[edge[i].v];
                    cur[u]=i;
                }
            }
            d[u]=mind+1;
            num[d[u]]++;
            u=pre[u];
        }
    }
    printf("%d\n",flow);
}
int main()
{
    int m,i;
    int a,b,c;
    scanf("%d %d",&n,&m);
    s=0;
    t=n+1;
    nv=t+1;
    cnt=0;
    memset(head,-1,sizeof(head));
    for(i=1;i<=n;i++){
        scanf("%d %d",&a,&b);
        add(s,i,a);
        add(i,t,b);
    }
    while(m--){
        scanf("%d %d %d",&a,&b,&c);
        add(a,b,c);
        add(b,a,c);
    }
    isap();
    return 0;
}



POJ 3469-Dual Core CPU(网络流_最小割求容量)

标签:

原文地址:http://blog.csdn.net/u013486414/article/details/43149715

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!