poj3469 Dual Core CPU-最小花费-最大流最小割

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



题意

n个任务,两个cpu A,B,输入n对数字Ai Bi表示i号任务在A上的花费和在B上的花费。

然后m行,每行三个数字。表示如果a任务和b任务不在一个cpu工作的话,需要额外花费c。

问,所有任务都工作的情况下,最小的花费是多少。


解题思路

如果没有额外花费,只要每个选最小就好了,但是牵扯到额外花费,就不行了,如果是没有额外花费,只需要由起点向每个任务连一条边,每个任务再向终点连一条边就可以了,这样,因为每个任务都向终点连了一条边,所以每个边都会有一定的流量流入终点t,而流量则取决于从s到这个任务的边和从任务到t的边中权值小的那个。

那么额外费用怎么办?

如果我们向两个有额外花费的任务连一个双向边,权值为花费。如图

本来任务3应该选择消耗为3的路,但是有了权值为1000的边之后就可以s->2->3->t 走权值为10的边了,这就相当于本来用cpuA执行任务,现在选择更优的cupB执行任务了。


代码

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>

using namespace std;
const int maxn = 200000+10;
const int INF = 0x3f3f3f3f;

struct edge
{
    int to,cap,rev;
};

vector <edge> G[maxn];
int level[maxn],iter[maxn];

void add_edge(int from,int to,int cap)
{
    G[from].push_back((edge){to,cap,G[to].size()});
    G[to].push_back((edge){from,0,G[from].size()-1});
}

void bfs(int s)
{
    memset(level,-1,sizeof(level));
    queue<int> q;
    level[s]=0;
    q.push(s);
    while(!q.empty())
    {
        int v = q.front();
        q.pop();
        for(int i=0;i<G[v].size();++i)
        {
            edge &e = G[v][i];
            if(e.cap>0 && level[e.to]<0)
            {
                level[e.to]=level[v]+1;
                q.push(e.to);
            }
        }
    }
}

int dfs(int v,int t,int f)
{
    if(v==t) return f;
    for(int &i=iter[v];i<G[v].size();++i)
    {
        edge &e = G[v][i];
        if(e.cap>0 && level[v]<level[e.to])
        {
            int d = dfs(e.to,t,min(f,e.cap));
            if(d>0)
            {
                e.cap -= d;
                G[e.to][e.rev].cap += d;
                return d;
            }
        }
    }
    return 0;
}

int max_flow(int s,int t)
{
    int flow = 0;
    while(1)
    {
        bfs(s);
        if(level[t]<0) return flow;
        memset(iter,0,sizeof(iter));
        int f;
        while((f=dfs(s,t,INF))>0)
        {
            flow+=f;
        }
    }
}

int n,m;
int s,t;
int a[maxn],b[maxn],w[maxn];//a,b里存的是分别在A,B类cup运行的花费
int main()
{
    scanf("%d%d",&n,&m);
    s = n,t = s+1;
    for(int i=0;i<n;++i)
    {
        scanf("%d%d",&a[i],&b[i]);
        add_edge(i,t,a[i]);
        add_edge(s,i,b[i]);
    }
    for(int i=0;i<m;++i)
    {
        int c,d;
        scanf("%d%d%d",&c,&d,&w[i]);
        add_edge(c-1,d-1,w[i]);
        add_edge(d-1,c-1,w[i]);
    }

    printf("%d\n",max_flow(s,t));
    return 0;
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值