HDU 3667 Transportation(最小费用最大流)

题意:有N个节点M条边的有向图,现在你需要从1号节点运送k个货物到N号节点. 每条边都有一个ai和ci值,ci值是指该边最多能运ci个货物,而你如果在该边运x(1<=x<=ci)个货物需要花费ai*x*x代价.问你运送这k个货物的最小代价是多少?

思路:这里显然是最小费用最大流的问题,问题是每条边的权值为一个函数,大白书上有讲,举个例子,比如最多能运4个货物,那么在该边分别运1,2,3,4个货物的代价就是ai*1,ai*4,ai*9,ai*16,那么相减一下有ai*1,ai*3,ai*5,ai*7,显然运送几个货物的代价就是这个等差数列的前n项和,那么把最多可以运送c件货物拆成c条边,每条边容量为1,权值为ai*(2*i-1),这样费用流的时候第一次就会选权值最小的那条边,第二次就会选次小的那条边,把结果累加就是前n项和了,非常巧妙的拆边建图。


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#define INF 1e9
using namespace std;
const int maxn=100+5;

struct Edge
{
    int from,to,cap,flow,cost;
    Edge(){}
    Edge(int f,int t,int c,int fl,int co):from(f),to(t),cap(c),flow(fl),cost(co){}
};

struct MCMF
{
    int n,m,s,t;
    vector<Edge> edges;
    vector<int> G[maxn];
    bool inq[maxn];
    int d[maxn];
    int a[maxn];
    int p[maxn];

    void init(int n,int s,int t)
    {
        this->n=n, this->s=s, this->t=t;
        edges.clear();
        for(int i=0;i<n;++i) G[i].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 &flow,int &cost)
    {
        queue<int> Q;
        for(int i=0;i<n;++i) d[i]=INF;
        memset(inq,0,sizeof(inq));
        d[s]=0,a[s]=INF,p[s]=0,inq[s]=true,Q.push(s);

        while(!Q.empty())
        {
            int u=Q.front(); Q.pop();
            inq[u]=false;
            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;
                    a[e.to] = min(a[u],e.cap-e.flow);
                    p[e.to] = G[u][i];
                    if(!inq[e.to]) {inq[e.to]=true; Q.push(e.to);}
                }
            }
        }
        if(d[t]==INF) return false;
        flow += a[t];
        cost += d[t]*a[t];
        int u=t;
        while(u!=s)
        {
            edges[p[u]].flow += a[t];
            edges[p[u]^1].flow -=a[t];
            u = edges[p[u]].from;
        }
        return true;
    }

    int solve(int k)
    {
        int flow=0,cost=0;
        while(BellmanFord(flow,cost));
        return flow==k? cost:-1;
    }
}MM;


int main()
{
    int n,m,k;
    while(scanf("%d%d%d",&n,&m,&k)==3)
    {
        int src=0,dst=n;
        MM.init(n+1,src,dst);
        MM.AddEdge(src,1,k,0);
        while(m--)
        {
            int u,v,a,c;
            scanf("%d%d%d%d",&u,&v,&a,&c);
            for(int i=1;i<=c;++i)
                MM.AddEdge(u,v,1,a*(2*i-1));
        }
        printf("%d\n",MM.solve(k));
    }
    return 0;
}

Description

There are N cities, and M directed roads connecting them. Now you want to transport K units of goods from city 1 to city N. There are many robbers on the road, so you must be very careful. The more goods you carry, the more dangerous it is. To be more specific, for each road i, there is a coefficient a  i. If you want to carry x units of goods along this road, you should pay a  i * x  2 dollars to hire guards to protect your goods. And what’s worse, for each road i, there is an upper bound C  i, which means that you cannot transport more than C i units of goods along this road. Please note you can only carry integral unit of goods along each road. 
You should find out the minimum cost to transport all the goods safely. 
 

Input

There are several test cases. The first line of each case contains three integers, N, M and K. (1 <= N <= 100, 1 <= M <= 5000, 0 <= K <= 100). Then M lines followed, each contains four integers (u  i, v  i, a  i, C  i), indicating there is a directed road from city u  i to v  i, whose coefficient is a  i and upper bound is C  i. (1 <= u  i, v  i <= N, 0 < a  i <= 100, C  i <= 5)
 

Output

Output one line for each test case, indicating the minimum cost. If it is impossible to transport all the K units of goods, output -1. 

 

Sample Input

       
       
2 1 2 1 2 1 2 2 1 2 1 2 1 1 2 2 2 1 2 1 2 1 2 2 2
 

Sample Output

       
       
4 -1 3
 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值