最小费用最大流

SPOJ 371 BOXES 附带最终版MCMF模板

Posted on

Table of contents

非常感谢这道题,准确的来说应该是这道题的数据。这道题其实是很简单的。

题意:
一堆箱子围成一个圈,每个箱子里面初始没有或有几个球。要求最后每个箱子里面最多一个球。每次转移球只能转移到相邻的箱子,转移一个球,需要花费1。问最小花费。

思路:
最暴力的思路是每个球对其他所有球都建立一条边,费用为最短距离。
但是有个显然的特点是,a -> b 与 a-> k , k-> b 的花费是相同的。所以我们只要在相邻的箱子建边即可。容量 inf ,费用为 1 。
建立源点连接有球的箱子,容量为 球 数,花费为 0 。箱子到汇点容量为 1 ,花费为 0 。

AC Code

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
using namespace std;

const int inf = 0x3f3f3f3f;
const int maxn = 2005;
const int maxe = 40005;

struct MCMF {
private:
    int src, des, idx, ans, flow;
    int head[maxn], dis[maxn], preID[maxe], que[maxe + 10];
    bool inq[maxn];

    struct node {
        int to, next;
        int flow, cost;
        node() {}
        node(int _to, int _next, int _flow, int _cost)
            : to(_to)
            , next(_next)
            , flow(_flow)
            , cost(_cost)
        {
        }
    } edges[maxe];

    bool spfa()
    {
        for (int i = src; i <= des; i++) {
            dis[i] = inf;
            inq[i] = false;
            preID[i] = -1;
        }
        int u, l = 0, r = 1;
        inq[src] = true;
        dis[src] = 0;
        que[0] = src;
        while (l != r) {
            u = que[l++];
            if (l == maxe + 1)
                l = 0;
            for (int id = head[u]; ~id; id = edges[id].next) {
                int v = edges[id].to;
                if (edges[id].flow > 0 && dis[u] + edges[id].cost < dis[v]) {
                    dis[v] = dis[u] + edges[id].cost;
                    preID[v] = id;
                    if (!inq[v]) {
                        inq[v] = true;
                        if (dis[v] < dis[que[l]]) {
                            l--;
                            if (l == -1)
                                l = maxe;
                            que[l] = v;
                        } else {
                            que[r++] = v;
                            if (r == maxe + 1)
                                r = 0;
                        }
                    }
                }
            }
            inq[u] = false;
        }
        return dis[des] != inf;
    }

public:
    void addEdge(int u, int v, int f, int c)
    {
        edges[idx] = node(v, head[u], f, c);
        head[u] = idx++;
        edges[idx] = node(u, head[v], 0, -c);
        head[v] = idx++;
    }

    void init(int s, int e)
    {
        src = s, des = e;
        memset(head, -1, sizeof head);
        flow = ans = idx = 0;
    }

    int minCost()
    {
        while (spfa()) {
            int mflow = inf;
            for (int id = preID[des]; id != -1; id = preID[edges[id ^ 1].to])
                mflow = min(mflow, edges[id].flow);
            for (int id = preID[des]; id != -1; id = preID[edges[id ^ 1].to]) {
                ans += mflow * edges[id].cost;
                edges[id].flow -= mflow;
                edges[id ^ 1].flow += mflow;
            }
            flow += mflow;
        }
        return ans;
    }
} mcmf;

int main()
{
    int T, n, num;
    scanf("%d", &T);
    while (T--) {
        scanf("%d", &n);
        mcmf.init(0, n + 1);
        for (int i = 1; i <= n; i++) {
            scanf("%d", &num);
            if (num)
                mcmf.addEdge(0, i, num, 0);
            mcmf.addEdge(i, n + 1, 1, 0);
            mcmf.addEdge(i, i % n + 1, inf, 1);
            mcmf.addEdge(i % n + 1, i, inf, 1);
        }
        printf("%d\n", mcmf.minCost());
    }
    return 0;
}

MCMF模板

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
using namespace std;

const int inf = 0x3f3f3f3f;
const int maxn = 2005;
const int maxe = 40005;

struct MCMF {
private:
    int src, des, idx, ans, flow;
    int head[maxn], dis[maxn], preID[maxe], que[maxe + 10];
    bool inq[maxn];

    struct node {
        int to, next;
        int flow, cost;
        node() {}
        node(int _to, int _next, int _flow, int _cost)
            : to(_to)
            , next(_next)
            , flow(_flow)
            , cost(_cost)
        {
        }
    } edges[maxe];

    bool spfa()
    {
        for (int i = src; i <= des; i++) {
            dis[i] = inf;
            inq[i] = false;
            preID[i] = -1;
        }
        int u, l = 0, r = 1;
        inq[src] = true;
        dis[src] = 0;
        que[0] = src;
        while (l != r) {
            u = que[l++];
            if (l == maxe + 1)
                l = 0;
            for (int id = head[u]; ~id; id = edges[id].next) {
                int v = edges[id].to;
                if (edges[id].flow > 0 && dis[u] + edges[id].cost < dis[v]) {
                    dis[v] = dis[u] + edges[id].cost;
                    preID[v] = id;
                    if (!inq[v]) {
                        inq[v] = true;
                        if (dis[v] < dis[que[l]]) {
                            l--;
                            if (l == -1)
                                l = maxe;
                            que[l] = v;
                        } else {
                            que[r++] = v;
                            if (r == maxe + 1)
                                r = 0;
                        }
                    }
                }
            }
            inq[u] = false;
        }
        return dis[des] != inf;
    }

public:
    void addEdge(int u, int v, int f, int c)
    {
        edges[idx] = node(v, head[u], f, c);
        head[u] = idx++;
        edges[idx] = node(u, head[v], 0, -c);
        head[v] = idx++;
    }

    void init(int s, int e)
    {
        src = s, des = e;
        memset(head, -1, sizeof head);
        flow = ans = idx = 0;
    }

    int minCost()
    {
        while (spfa()) {
            int mflow = inf;
            for (int id = preID[des]; id != -1; id = preID[edges[id ^ 1].to])
                mflow = min(mflow, edges[id].flow);
            for (int id = preID[des]; id != -1; id = preID[edges[id ^ 1].to]) {
                ans += mflow * edges[id].cost;
                edges[id].flow -= mflow;
                edges[id ^ 1].flow += mflow;
            }
            flow += mflow;
        }
        return ans;
    }
} mcmf;
最小费用最大流

CSU 1506 Double Shortest Paths

Posted on

算是康复福利了,以前没做过,但是实际是非常水的题目,应该可以作为入门题了

诡异的是 这道题description莫名奇妙没了。

题意:
两个人从 1 -> n ,每条路径最多走两次,走第一次为 d ,走第二次为 d+a ,问最短的路径。

思路:
对于一组数据,建两次边即可。容量为1,花费分别为 d , d + a。
源点流入1 容量为 2 , 花费 0
n流入汇点 容量 2 ,花费 0

注: 给定的边为有向边

AC code

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

const int inf = 99999999;
const int maxn = 1005;
const int maxe = 10005 * 4;

struct MCMF {
private:
    int st, en, idx;
    int head[maxn], dis[maxe], preID[maxe];
    bool vis[maxe];
    queue<int> que;

    struct node {
        int to, next, pre;
        int flow, cost;
        node() {}
        node(int _to, int _next, int _pre, int _flow, int _cost)
            : to(_to)
            , next(_next)
            , pre(_pre)
            , flow(_flow)
            , cost(_cost)
        {
        }
    } edges[maxe];

    bool spfa()
    {
        memset(vis, false, sizeof(vis));
        for (int i = st; i <= en; i++)
            dis[i] = inf;
        vis[st] = true;
        dis[st] = 0;
        que.push(st);
        int u;
        while (!que.empty()) {
            u = que.front();
            que.pop();
            for (int id = head[u]; ~id; id = edges[id].next) {
                int v = edges[id].to;
                if ((edges[id].flow > 0) && (dis[u] + edges[id].cost < dis[v])) {
                    dis[v] = dis[u] + edges[id].cost;
                    preID[v] = id;
                    if (!vis[v]) {
                        vis[v] = true;
                        que.push(v);
                    }
                }
            }
            vis[u] = false;
        }
        return dis[en] != inf;
    }

public:
    void addEdge(int u, int v, int f, int c)
    {
        edges[idx] = node(v, head[u], u, f, c);
        head[u] = idx++;
        edges[idx] = node(u, head[v], v, 0, -c);
        head[v] = idx++;
    }

    void init(int s, int e)
    {
        st = s, en = e;
        memset(head, -1, sizeof head);
        idx = 0;
    }

    int minCost()
    {
        int ans = 0;
        while (spfa()) {
            ans += dis[en];
            for (int id = preID[en]; id != st; id = preID[edges[id].pre]) {
                edges[id].flow--;
                edges[id ^ 1].flow++;
            }
        }
        return ans;
    }
} mcmf;

int main()
{
    int n, m, u, v, c, c1, cas = 0;
    while (scanf("%d %d", &n, &m) != EOF) {
        mcmf.init(0, n + 1);
        mcmf.addEdge(0, 1, 2, 0);
        mcmf.addEdge(n, n + 1, 2, 0);
        while (m--) {
            scanf("%d %d %d %d", &u, &v, &c, &c1);
            mcmf.addEdge(u, v, 1, c);
            mcmf.addEdge(u, v, 1, c + c1);
        }
        printf("Case %d: %d\n", ++cas, mcmf.minCost());
    }
    return 0;
}
最小费用最大流

POJ 2135 Farm Tour

Posted on

康复计划之 最小费用最大流
以前写的是直接在网上拿了一个模板,直接套了A……
这次好好在纸上模拟了一下,理解了一下。

最小费用最大流的增广路算法简单说就是不断求最短路(因为残量网络和反向边的存在),记录最短路,再进行流量处理,直到最后不存在通路,则停止。

下面是一个 入门题

题意:
给你一张图,要你从1 -> n 再从 n -> 1 不能走重复的路径。问你最短方案。

思路:
其实就是找出最短和次短的 1 -> n 的路径,当然路径不允许任何重叠。
单纯的两次spfa是不可行的。因为第一次最短路很可能会把通路断掉。

AC Code

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

const int inf = 99999999;
const int maxn = 1005;
const int maxe = 10005 * 4;

struct MCMF {
private:
    int st, en, idx;
    int head[maxn], dis[maxe], preID[maxe];
    bool vis[maxe];
    queue<int> que;

    struct node {
        int to, next, pre;
        int flow, cost;
        node() {}
        node(int _to, int _next, int _pre, int _flow, int _cost)
            : to(_to)
            , next(_next)
            , pre(_pre)
            , flow(_flow)
            , cost(_cost)
        {
        }
    } edges[maxe];

    bool spfa()
    {
        memset(vis, false, sizeof(vis));
        for (int i = st; i <= en; i++)
            dis[i] = inf;
        vis[st] = true;
        dis[st] = 0;
        que.push(st);
        int u;
        while (!que.empty()) {
            u = que.front();
            que.pop();
            for (int id = head[u]; ~id; id = edges[id].next) {
                int v = edges[id].to;
                if ((edges[id].flow > 0) && (dis[u] + edges[id].cost < dis[v])) {
                    dis[v] = dis[u] + edges[id].cost;
                    preID[v] = id;
                    if (!vis[v]) {
                        vis[v] = true;
                        que.push(v);
                    }
                }
            }
            vis[u] = false;
        }
        return dis[en] != inf;
    }

public:
    void addEdge(int u, int v, int f, int c)
    {
        edges[idx] = node(v, head[u], u, f, c);
        head[u] = idx++;
        edges[idx] = node(u, head[v], v, 0, -c);
        head[v] = idx++;
    }

    void init(int s, int e)
    {
        st = s, en = e;
        memset(head, -1, sizeof head);
        idx = 0;
    }

    int minCost()
    {
        int ans = 0;
        while (spfa()) {
            ans += dis[en];
            for (int id = preID[en]; id != st; id = preID[edges[id].pre]) {
                edges[id].flow--;
                edges[id ^ 1].flow++;
            }
        }
        return ans;
    }
} mcmf;

int main()
{
    int n, m;
    scanf("%d%d", &n, &m);
    mcmf.init(0, n + 1);
    mcmf.addEdge(0, 1, 2, 0);
    mcmf.addEdge(n, n + 1, 2, 0);
    for (int i = 1; i <= m; i++) {
        int u, v, c;
        scanf("%d%d%d", &u, &v, &c);
        mcmf.addEdge(u, v, 1, c);
        mcmf.addEdge(v, u, 1, c);
    }
    printf("%d\n", mcmf.minCost());
    return 0;
}