• POJ 3469 Dual Core CPU (最小割建模)


    题意

    现在有n个任务,两个机器A和B,每个任务要么在A上完成,要么在B上完成,而且知道每个任务在A和B机器上完成所需要的费用。然后再给m行,每行 a,b,w三个数字。表示如果a任务和b任务不在同一个机器上工作的话,需要额外花费w。现在要求出完成所有任务最小的花费是多少。

    思路

    上次做的构图题是基于割截断s->t流与题目联系的,而这道题的构图则是基于割把流网络的点划分成了S、T点集,并且不同点集间的边都是割边。这是目前我所接触到的最小割的建模类型。
    回到本题构图:源点向任务连一条Ai容量的边,任务向汇点连一条Bi容量的边,如果两任务在不同Core上需要代价则连一条(i,j,cost)的无向边。

    代码

     
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #define MID(x,y) ((x+y)/2)
    #define mem(a,b) memset(a,b,sizeof(a))
    using namespace std;
    const int MAXV = 505;
    const int MAXE = 20005;
    const int oo = 0x3fffffff;
    struct node{
        int u, v, flow;
        int opp;
        int next;
    };
    struct Dinic{
        node arc[MAXE];
        int vn, en, head[MAXV];     //vn点个数(包括源点汇点),en边个数
        int cur[MAXV];              //当前弧
        int q[MAXV];                //bfs建层次图时的队列
        int path[MAXE], top;        //存dfs当前最短路径的栈
        int dep[MAXV];              //各节点层次
        void init(int n){
            vn = n;
            en = 0;
            mem(head, -1);
        }
        void insert_flow(int u, int v, int flow){
            arc[en].u = u;
            arc[en].v = v;
            arc[en].flow = flow;
            arc[en].opp = en + 1;
            arc[en].next = head[u];
            head[u] = en ++;
    
            arc[en].u = v;
            arc[en].v = u;
            arc[en].flow = 0;       //反向弧
            arc[en].opp = en - 1;
            arc[en].next = head[v];
            head[v] = en ++;
        }
        bool bfs(int s, int t){
            mem(dep, -1);
            int lq = 0, rq = 1;
            dep[s] = 0;
            q[lq] = s;
            while(lq < rq){
                int u = q[lq ++];
                if (u == t){
                    return true;
                }
                for (int i = head[u]; i != -1; i = arc[i].next){
                    int v = arc[i].v;
                    if (dep[v] == -1 && arc[i].flow > 0){
                        dep[v] = dep[u] + 1;
                        q[rq ++] = v;
                    }
                }
            }
            return false;
        }
        int solve(int s, int t){
            int maxflow = 0;
            while(bfs(s, t)){
                int i, j;
                for (i = 1; i <= vn; i ++)  cur[i] = head[i];
                for (i = s, top = 0;;){
                    if (i == t){
                        int mink;
                        int minflow = 0x3fffffff;
                        for (int k = 0; k < top; k ++)
                            if (minflow > arc[path[k]].flow){
                                minflow = arc[path[k]].flow;
                                mink = k;
                            }
                        for (int k = 0; k < top; k ++)
                            arc[path[k]].flow -= minflow, arc[arc[path[k]].opp].flow += minflow;
                        maxflow += minflow;
                        top = mink;		//arc[mink]这条边流量变为0, 则直接回溯到该边的起点即可(这条边将不再包含在增广路内).
                        i = arc[path[top]].u;
                    }
                    for (j = cur[i]; j != -1; cur[i] = j = arc[j].next){
                        int v = arc[j].v;
                        if (arc[j].flow && dep[v] == dep[i] + 1)
                            break;
                    }
                    if (j != -1){
                        path[top ++] = j;
                        i = arc[j].v;
                    }
                    else{
                        if (top == 0)   break;
                        dep[i] = -1;
                        i = arc[path[-- top]].u;
                    }
                }
            }
            return maxflow;
        }
    }dinic;
    bool vis[MAXV];
    bool reach(int u, int p){
        vis[u] = 1;
        if (u == p)
            return true;
        for (int i = dinic.head[u]; i != -1; i = dinic.arc[i].next){
            if (i % 2 == 1) continue;
            int v = dinic.arc[i].v;
            if (vis[v] || dinic.arc[i].flow <= 0) continue;
            if (reach(v, p))
                return true;
        }
        return false;
    }
    int work(int n){
        vector  seg;
        for (int i = 0; i < dinic.en; i += 2){
            if (dinic.arc[i].flow == 0){
                mem(vis, 0);
                int u = dinic.arc[i].u;
                int v = dinic.arc[i].v;
                if (reach(n+1, u) && reach(v, n+2)){
                    seg.push_back(i/2+1);
                }
            }
        }
        for (int i = 0; i < (int)seg.size(); i ++){
            if (i == 0)
                printf("%d", seg[i]);
            else
                printf(" %d", seg[i]);
        }
        puts("");
    }
    int main(){
    	//freopen("test.in", "r", stdin);
    	//freopen("test.out", "w", stdout);
        int n, m, l;
        while(scanf("%d %d %d", &n, &m, &l), n){
            dinic.init(n+m+2);
            for (int i = 0; i < l; i ++){
                int u,v,w;
                scanf("%d %d %d", &u, &v, &w);
                dinic.insert_flow(u==0?n+m+2:u, v==0?n+m+2:v, w);
            }
            for (int i = 1; i <= n; i ++){
                dinic.insert_flow(n+m+1, i, oo);
            }
            dinic.solve(n+m+1, n+m+2);
            work(n+m);
        }
    	return 0;
    }
    
  • 相关阅读:
    android学习笔记07(activity跳转,通信,及发短信)
    android学习笔记05(RadioGroup,CheckBox,Toast)
    义无返顾
    android学习笔记01(LinearLayout)
    linux远程桌面学习
    android学习笔记08(activity通信的一个实例乘法计算器)
    android学习笔记12(ProgressBar进度条初级学习)
    android学习笔记06(第一个程序)
    android学习笔记03(RelativeLayout)
    poj2886 Who Gets the Most Candies?
  • 原文地址:https://www.cnblogs.com/AbandonZHANG/p/4114278.html
Copyright © 2020-2023  润新知