uva10594

Posted 啊嘞

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了uva10594相关的知识,希望对你有一定的参考价值。

题目链接请戳 这里

 

解题思路

用最小费用最大流。

注意最后的费用会是long long级别。

 

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;

const int maxn = 110;
const int maxm = 5010;
const long long INF = 1e16;    //因为最后费用可能很大,所以用long long 

struct Edge {
    int from, to, cap, flow, cost;
    Edge(int u, int v, int c, int f, int w):from(u), to(v), cap(c), flow(f), cost(w) {}
};

//这里用的是lrj紫书里的模板 
struct MCMF {
    int n, m;
    vector<Edge> edges;
    vector<int> G[maxn];
    int inq[maxn];
    long long d[maxn];
    int p[maxn];
    int a[maxn];
    
    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, long long &cost) {
        for (int i = 0; i <= n; i++) d[i] = INF;
        memset(inq, 0, sizeof(inq));
        d[s] = 0; inq[s] = 1; p[s] = -1; a[s] = 1000000000;
        
        queue<int> Q;
        Q.push(s);
        while (!Q.empty()) {
            int u = Q.front(); Q.pop();
            inq[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];
                    a[e.to] = min(a[u], e.cap - e.flow);
                    if (!inq[e.to]) { Q.push(e.to); inq[e.to] = 1; }
                }
            }
        }
        if (d[t] == INF) return false;
        flow += a[t];
        cost += (long long)d[t] * (long long)a[t];
        for (int u = t; u != s; u = edges[p[u]].from) {
            edges[p[u]].flow += a[t];
            edges[p[u] ^ 1].flow -= a[t];
        }
        return true;
    }
    
    int MincostMaxflow(int s, int t, long long &cost) {
        int flow = 0; cost = 0;
        while (BellmanFord(s, t, flow, cost));
        return flow;
    }
};

int from[maxm], to[maxm];
long long w[maxm];

int main()
{
    int n, m;
    MCMF A;
    while (~scanf("%d%d", &n, &m)) {
        for (int i = 0; i < m; i++) scanf("%d%d%lld", &from[i], &to[i], &w[i]);
        int d, f, c;
        long long cost = 0;
        scanf("%d%d", &d, &c);
        A.init(n);
        //建立顶点标号为0的顶点,其到顶点1的容量为全部要传输的数据,费用为0 
        A.AddEdge(0, 1, d, 0);
        //因为为无向图,所以来回两条边都要加入 
        for (int i = 0; i < m; i++) {
            A.AddEdge(from[i], to[i], c, w[i]);
            A.AddEdge(to[i], from[i], c, w[i]);
        }
        f = A.MincostMaxflow(0, n, cost);
        if (f != d) printf("Impossible.\n");
        else printf("%lld\n", cost);
    }
    return 0;
}

 

以上是关于uva10594的主要内容,如果未能解决你的问题,请参考以下文章

UVa10410代码

UVa 1593代码对齐

UVa 1593 代码对齐

如何查看端口recv和send

算法习题---5.1代码对齐(UVa1593)

HDU 1081 To The Max