码迷,mamicode.com
首页 > 其他好文 > 详细

UVALive - 5095 Transportation(拆边+费用流)

时间:2015-08-31 10:12:49      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:

题目大意:有n个点,m条边,每条边的容量为ci,费用为ai* x^2(x为流量,ai为所给系数)
现在问能否将k个单位的货物从点1运输到点n,且费用最小

解题思路:拆边,将每条边拆成ci条边,每条边的费用分别为ai * 1, ai * 3, ai * 5…容量都为1,在容量相同的情况下,会选择费用少的流,这样流过的边累加起来的费用刚好为 ai * 流量^2

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
#define N 1010
#define INF 0x3f3f3f3f

struct Edge{
    int from, to, cap, flow ,cost;
    Edge() {}
    Edge(int from, int to, int cap, int flow, int cost):from(from), to(to), cap(cap), flow(flow), cost(cost) {}
};

struct MCMF{
    int n, m, source, sink;
    int flow, cost;
    vector<Edge> edges;
    vector<int> G[N];
    int d[N], f[N], p[N];
    bool vis[N];

    void init(int n) {
        this->n = n;
        for (int i = 0; i <= n; i++)
            G[i].clear();
        edges.clear();
    }

    void AddEdge(int from, int to, int cap, int cost) {
        edges.push_back(Edge(from, to, cap, 0, cost));
        edges.push_back(Edge(to, from, 0, 0, -cost));
        m = edges.size();
        G[from].push_back(m - 2);
        G[to].push_back(m - 1);
    }

    bool BellmanFord(int s, int t, int &flow, int &cost, int need) {
        for (int i = 0; i <= n; i++)
            d[i] = INF;
        memset(vis, 0, sizeof(vis));
        vis[s] = 1; d[s] = 0; f[s] = need; p[s] = 0;
        queue<int> Q;
        Q.push(s);

        while (!Q.empty()) {
            int u = Q.front();
            Q.pop();
            vis[u] = 0;

            for (int i = 0; i < G[u].size(); i++) {
                Edge &e = edges[G[u][i]];
                if (e.cap > e.flow && d[e.to] > d[u] + e.cost) {
                    d[e.to] = d[u] + e.cost;
                    p[e.to] = G[u][i];
                    f[e.to] = min(f[u], e.cap - e.flow);
                    if (!vis[e.to]) {
                        vis[e.to] = true;
                        Q.push(e.to);
                    }
                }
            }
        }

        if (d[t] == INF)
            return false;

        flow += f[t];
        cost += d[t];

        int u = t;
        while (u != s) {
            edges[p[u]].flow += f[t];
            edges[p[u] ^ 1].flow -= f[t];
            u = edges[p[u]].from;
        }
        return true;
    }

    void Mincost(int s, int t, int need) {
        flow = 0, cost = 0;
        while (BellmanFord(s, t, flow, cost, need - flow)) {
            if (need == flow) break;
        }
    }
};
MCMF mcmf;
int n, m, k;
int num[6] = {0, 1, 3, 5, 7, 9};

void solve() {
    int u, v, a, c;
    mcmf.init(n);
    for (int i = 0; i < m; i++) {
        scanf("%d%d%d%d", &u, &v, &a, &c);
        for (int j = 1; j <= c; j++) mcmf.AddEdge(u, v, 1, num[j] * a);
    }

    mcmf.Mincost(1, n, k);
    if (mcmf.flow == k) 
        printf("%d\n", mcmf.cost);
    else
        printf("-1\n");
}

int main() {
    while (scanf("%d%d%d", &n, &m, &k) != EOF) solve();
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

UVALive - 5095 Transportation(拆边+费用流)

标签:

原文地址:http://blog.csdn.net/l123012013048/article/details/48129121

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!