• [题解] PowerOJ 1754 负载平衡问题 (最小费用最大流)


    - 传送门 -

     https://www.oj.swust.edu.cn/problem/show/1754

    #1754: 负载平衡问题

    Time Limit: 1000 MS Memory Limit: 65536 KB
    Total Submit: 77 Accepted: 54 Page View: 427

    Description

    G 公司有n 个沿铁路运输线环形排列的仓库,每个仓库存储的货物数量不等。如何用最 少搬运量可以使n 个仓库的库存数量相同。搬运货物时,只能在相邻的仓库之间搬运。 编程任务: 对于给定的n 个环形排列的仓库的库存量,编程计算使n 个仓库的库存数量相同的最少 搬运量。

    Input

    由文件input.txt 提供输入数据。文件的第1 行中有1 个正整数n(n<=100),表示有n 个仓库。第2 行中有n个正整数,表示n个仓库的库存量。

    Output

    程序运行结束时,将计算出的最少搬运量输出到文件output.txt中。

    5
    17 9 14 16 4

    11

    Source

    线性规划与网络流24题

     

    - 思路 -

     一个仓库用X, Y 两个集合来表示, 保留不动的部分通过 X 到 Y 的对应节点的容量inf , 费用 0 的边传输, 源汇点就不讲了, 重点在于表示仓库间的运输的是从 Y 集合连向 X 集合的费用为 1 的边.
     
     细节见代码.
     

    - 代码 -

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<queue>
    using namespace std;
     
    const int N = 2e3 + 5;
    const int M = 1e3 + 5;
    const int inf = 0x3f3f3f3f;
     
    int NXT[M], TO[M], V[M], CT[M];
    int HD[N], DIS[N], VIS[N], A[N], PRE[N];
    int ss, tt, n, sz, tmp;
    queue<int> q;
     
    void add(int x, int y, int z, int c) {
        TO[sz] = y; V[sz] = z; CT[sz] = c;
        NXT[sz] = HD[x]; HD[x] = sz++;
        TO[sz] = x; V[sz] = 0; CT[sz] = -c;
        NXT[sz] = HD[y]; HD[y] = sz++;
    }
     
    bool spfa() {
        memset(DIS, 0x3f, sizeof (DIS));
        q.push(ss);
        DIS[ss] = 0;
        while (!q.empty()) {
            int u = q.front();
            q.pop();
            VIS[u] = 0;
            for (int i = HD[u]; i != -1; i = NXT[i]) {
                int v = TO[i];
                if (V[i] && DIS[v] > DIS[u] + CT[i]) {
                    DIS[v] = DIS[u] + CT[i];
                    PRE[v] = i;
                    if (!VIS[v]) {
                        VIS[v] = 1;
                        q.push(v);
                    }
                }
            }
        }
        return DIS[tt] != inf;
    }
     
    int mcmf() {
        int cost = 0;
        while (spfa()) {
            int tmp = inf;
            for (int i = tt; i != ss; i = TO[PRE[i]^1])
                if (tmp > V[PRE[i]]) tmp = V[PRE[i]];
            for (int i = tt; i != ss; i = TO[PRE[i]^1]) {
                V[PRE[i]] -= tmp;
                V[PRE[i]^1] += tmp;
                cost += tmp * CT[PRE[i]];
            }
        }
        return cost;
    }
     
    int main() {
        memset(HD, -1, sizeof (HD));
        scanf("%d", &n);
        ss = 0, tt = n * 2 + 1;
        for (int i = 1, x; i <= n; ++i) {
            scanf("%d", &A[i]);
            tmp += A[i];
        }
        tmp /= n;
        for (int i = 1; i <= n; ++i) {
            add(ss, i, A[i], 0);
            add(n + i, tt, tmp, 0);
            add(i, n + i, inf, 0);
            int x = n + i + 1, y = n + i - 1;
            if (i == n) x = n + 1; add(x, i, inf, 1);
            if (i == 1) y = n * 2; add(y, i, inf, 1);
        }
        printf("%d
    ", mcmf());
        return 0;
    }
    
  • 相关阅读:
    卡尔曼滤波的原理与思想
    什么是端口
    什么是ICD文件
    USB口,串口,以太网口简介
    Linux基本操作笔记
    网络营销学习路线图
    读书推荐:2017 第一期
    又见Bug
    如何请教别人问题?
    圆桌问答 (2017 第一季)
  • 原文地址:https://www.cnblogs.com/Anding-16/p/7422329.html
Copyright © 2020-2023  润新知