zoukankan      html  css  js  c++  java
  • [poj3469]Dual Core CPU(最小割)

    题目大意:给你$n$个模块,每个模块在A核花费为$a_{i}$,在B核跑花费为$b_{i}$,然后由$m$个任务$(a_{i},b_{i},w_{i})$,表示如果$a_{i},b_{i}$不在同一个核上跑,额外的花费为$w_{i}$,求最小的花费。

    解题关键:此题的关键为建模,一个超级源点指向所有任务,容量为任务点在A上所花的代价,然后所有任务点指向一个超级汇点,容量为任务点在B上所花的代价,然后多花费代价$c$,就让$a$和$b$之间连一条边,来回的容量都为$c$。

    总结:用最小费用将对象划分成两个集合的问题,常常转换成最小割解决.

    ***仔细考虑,是一个经典最小割模型,用删边来考虑意义

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cstdlib>
    #include<cmath>
    #include<iostream>
    #include<queue>
    #include<vector>
    #define inf 0x3f3f3f3f
    #define MAX_V 20002
    using namespace std;
    typedef long long ll;
    struct edge{int to,cap,rev;};
    vector<edge>G[MAX_V];
    int level[MAX_V],iter[MAX_V];
    void add_edge(int from,int to,int cap){
        G[from].push_back((edge){to,cap,(int)G[to].size()});
        G[to].push_back((edge){from,0,(int)G[from].size()-1});
    }
    void bfs(int s){
        memset(level,-1,sizeof level);
        queue<int>que;
        level[s]=0;
        que.push(s);
        while(!que.empty()){
            int v=que.front();que.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;
                    que.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 dinic(int s,int t){
        int flow=0,f;
        while(1){
            bfs(s);
            if(level[t]<0) return flow;
            memset(iter,0,sizeof iter);
            while((f=dfs(s,t,inf))>0){
                flow+=f;
            }
        }
        return flow;
    }
    int n,m,a,b,s,t,w;
    int main(){
        while(scanf("%d%d",&n,&m)!=EOF){
            s=0,t=n+1;
            for(int i=1;i<=n;i++){
                scanf("%d%d",&a,&b);
                add_edge(i,t,a);
                add_edge(s,i,b);
            }
            for(int i=1;i<=m;i++){
                scanf("%d%d%d",&a,&b,&w);
                add_edge(a, b, w);
                add_edge(b, a, w);
            }
            printf("%d
    ",dinic(s, t));
        }
        return 0;
    }
  • 相关阅读:
    【学习笔记/题解】树上启发式合并/CF600E Lomsat gelral
    【学习笔记/题解】虚树/[SDOI2011]消耗战
    【题解】 [GZOI2017]小z玩游戏
    【题解】CF1426E Rock, Paper, Scissors
    【题解】CF1426D Non-zero Segments
    【题解】NOIP2018 填数游戏
    【题解】NOIP2018 旅行
    【题解】NOIP2018 赛道修建
    【题解】时间复杂度
    【题解】「MCOI-02」Convex Hull 凸包
  • 原文地址:https://www.cnblogs.com/elpsycongroo/p/7907004.html
Copyright © 2011-2022 走看看