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

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
题意:有n个城市,m条单向路。现在运送货物,每条路有一个系数ai有最多能运的货物数ci,费用关系为val=ai*x*x,(其中x为运送货物数,x<=ci),问你把K件货物从1城市运到n城市最少需要多少费用,如果不能运完输出-1。
分析:都会想到根据所给的边建图,关键是怎么解决费用问题,因为费用是个函数关系,这一点其实大白书上有说过如何建图。我们可以举个例子看看:假如货物系数为ai,现在运送的货物从1件到4件费用分别是:ai*1,ai*4,ai*9,ai*16,两两相减得到:ai*1,ai*3,ai*5,ai*7...意思就是运送货物为x件,就是这个等差数列的前x项和,那么我们就可以建边了,假如x城市到y城市路的系数为ai,容量为c,那么我们可以从x到y建c条容量为1的边,代表可以走c次,满足容量问题,而每一条边的费用依次为ai*1,ai*3,ai*5...,就这样,跑费用流的时候经过此路第一次就会选费用最小的那条,走第二次时会选择费用第二大的那条(因为是求最小费用,所以它自动会从小到大选择,并把结果累加,就实现了这个功能)。
#include<cstdio>
#include<string.h>
#include<queue>
#include<algorithm>
#define maxn 1100
#define inf 0x3f3f3f
using namespace std;
struct node
{
    int st;
    int en;
    int flow,cost;
    int next;
}E[maxn*maxn];
int num;
int p[maxn];
void init()
{
    memset(p,-1,sizeof p);
    num=0;
}
void add(int st,int en,int flow,int cost)
{
    E[num].st=st;
    E[num].en=en;
    E[num].flow=flow;
    E[num].cost=cost;
    E[num].next=p[st];
    p[st]=num++;
    E[num].st=en;
    E[num].en=st;
    E[num].flow=0;
    E[num].cost=-cost;
    E[num].next=p[en];
    p[en]=num++;
}
int pre[maxn];
int dis[maxn];
bool fg[maxn];

bool spfa(int st,int en)
{
    for(int i=0;i<=en;i++)
        fg[i]=0,dis[i]=inf,pre[i]=-1;
    queue<int>q;
    q.push(st);
    fg[st]=1;
    dis[st]=0;
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        fg[u]=0;
        for(int i=p[u];i+1;i=E[i].next)
        {
            int v=E[i].en;
            if(E[i].flow&&dis[v]>dis[u]+E[i].cost)
            {
                dis[v]=dis[u]+E[i].cost;
                pre[v]=i;
                if(!fg[v])
                {
                    fg[v]=1;
                    q.push(v);
                }
            }
        }
    }
    if(dis[en]<inf)
        return 1;
    return 0;
}
int f;
int solve(int st,int en)
{
    int ans=0;
    while(spfa(st,en))
    {
        f++;
        for(int i=pre[en];i+1;i=pre[E[i].st])
        {
            E[i].flow-=1;///每次跑完必为1,因为你的容量都是1
            E[i^1].flow+=1;
            ans+=E[i].cost;
        }
    }
    return ans;
}

int main()
{
    int n,m,k;
    while(scanf("%d%d%d",&n,&m,&k)!=EOF)
    {
        init();
        for(int i=1;i<=m;i++)
        {
            int x,y,a,c;
            scanf("%d%d%d%d",&x,&y,&a,&c);
            for(int j=1,cnt=1;cnt<=c;j+=2,cnt++) add(x,y,1,a*j);
        }
        add(n,n+1,k,0);///建一个汇点,限制它跑k次
        f=0;
        int ans=solve(1,n+1);
        if(f==k) printf("%d\n",ans);
        else puts("-1");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值