hdu3667 Transportation 费用与流量平方成正比的最小流 拆边法+最小费用最大流

/**
题目:hdu3667 Transportation 拆边法+最小费用最大流
链接:http://acm.hdu.edu.cn/showproblem.php?pid=3667
题意:n个城市由m条有向边连接。要从城市1运输k流量到城市n。每条边有可以运输的流量容量,以及费用系数ai。
费用系数指该条边的费用为该条边的运输流量x的平方乘以ai。即ai*x^2。

如果无法运输k流量,输出-1,否则输出从城市1运输k流量到城市n的最小花费。

思路:拆边法+最小费用最大流

假设从u->v 容量为5, 费用系数为a。

那么拆成五条容量为1的边,每条边的费用分别为 a,3*a,5*a,7*a,9*a。 (如果容量更大,拆成更多边,继续11*a,13*a...)

因为是最小费用流,如果当前流量为1,那么肯定经过费用为a(1*1*a = a)的这条边,如果流量为2,那么肯定经过费用为a和费用为3a的边(满足:x^2*ai = 2^2*a=4a);

其他类推。

然后就是普通的最小费用最大流。

由于本题是要输出运输k流量的最小流,所以增加源点s,汇点t。 s->1,cap=k,cost=0;  n->t,cap=k,cost=0; 求s到t的最小费用最大流即可。

如果最大流flow<k。那么-1。否则cost为正解。

*/
#include<iostream>
#include<cstring>
#include<vector>
#include<map>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
const int INF = 0x3f3f3f3f;
typedef long long LL;
const int N = 110;
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){}
};
struct MCMF{
    int n, m;
    vector<Edge> edges;
    vector<int> G[N];
    int inq[N];
    int d[N];
    int p[N];
    int a[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,long long 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] = 0; a[s] = INF;

        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 main()
{
    int n, m, k;
    while(scanf("%d%d%d",&n,&m,&k)==3)
    {
        int s = 0, t = n+1;
        MCMF mcmf;
        mcmf.init(t);
        mcmf.AddEdge(s,1,k,0);
        mcmf.AddEdge(n,t,k,0);
        int u, v, a, c;
        for(int i = 1; i <= m; i++){
            scanf("%d%d%d%d",&u,&v,&a,&c);
            for(int j = 1, cnt = 1; j <= c; j++, cnt+=2){
                mcmf.AddEdge(u,v,1,a*cnt);
            }
        }
        long long cost;
        int flow = mcmf.MincostMaxflow(s,t,cost);
        if(flow<k){
            printf("-1\n");
        }else
        {
            printf("%lld\n",cost);
        }
    }
    return 0;
}

 

posted on 2017-07-21 09:08  hnust_accqx  阅读(216)  评论(0编辑  收藏  举报

导航