POJ - 3469(最小割)

Dual Core CPU

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个任务需要分配给两个核心A和B,并且需要花费Ai或者Bi,最后有一些任务之间需要花费Wi传递数据如果他们不再同一个核心上运行;问解决所有任务最小花费是多少。

思路:“用最小的费用将对象分成两个集合的问题,常常可以转换成最小割后解决。考虑把N个模块按照在哪个核心上解决分成两个集合。再求最大流”(《挑战程序设计竞赛》)。以模块为顶点。s向每个模块连一条容量为Ai的线,每个模块向t连一条容量为Bi的线,最后考虑模块之间的数据传输费用。

#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <algorithm>
#include <functional>
#define inf 0X3f3f3f3f
using namespace std;
typedef long long ll;
const int MAXN=1e5+10;
const int MAX=200000+10;
const double eps=1e-6;

int N,M;
//dinic
//O(|E||V|)
int n;
struct EDGE{
    int v,c,rev;
};
vector<EDGE>G[MAX];
int level[MAX];//定点到原点的距离标号
int iter[MAX];//当前弧

void init(){
    for(int i=1;i<=N+10;i++)
        G[i].clear();
}

void addedge(int u,int v,int cap){
    G[u].push_back((EDGE){v,cap,G[v].size()});
    G[v].push_back((EDGE){u,0,G[u].size()-1});
}

//BFS计算从源点出发的距离标号
void bfs(int s){
    memset(level,-1,sizeof(level));
    queue<int>q;
    level[s]=0;
    q.push(s);
    while(q.size()){
        int u=q.front();q.pop();
        for(int i=0;i<G[u].size();i++){
            EDGE &e=G[u][i];
            if(e.c>0&&level[e.v]<0){
                level[e.v]=level[u]+1;
                q.push(e.v);
            }
        }
    }
}

//dfs寻找增广路
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.c>0&&level[v]<level[e.v]){
            int d=dfs(e.v,t,min(f,e.c));
            if(d>0){
                e.c-=d;
                G[e.v][e.rev].c+=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 main(){
    #ifdef ONLINE_JUDGE
    #else
    freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    #endif
    cin>>N>>M;
    init();
    int u,v,w1,w2,s,t;
    s=N+1;t=s+1;
    for(int i=1;i<=N;i++){
        scanf("%d%d",&w1,&w2);
        addedge(s,i,w1);
        addedge(i,t,w2);
    }
    for(int i=1;i<=M;i++){
        scanf("%d%d%d",&u,&v,&w1);
        addedge(u,v,w1);
        addedge(v,u,w1);
    }
    cout<<max_flow(s,t)<<endl;
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值