HDU 3667.Transportation 最小费用流

Transportation

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3045    Accepted Submission(s): 1318


Problem 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
 

 

Source
 

 

Recommend
lcy   |   We have carefully selected several similar problems for you:   3661  3664  3665  3669  3668 
 
题意:求从1运送K个货物到N最少花费,每条边有一个运送上限,运送费用为所有边x*x*w的和,其中x为这条边的运送货物量,w为价格。
思路:最小费用流。但是费用不是与货物成正比,而是与货物的平方成正比,所以不能直接跑最小费用最大流,最后用费用*流量*流量,所以需要建立新的模型。当流量为1是,费用为w,流量为2是,费用是4w,但流量为3时,费用是9w......。所以,可以建立一个这样的模型,跑1流量的花费为w,跑2流量的花费为4w,跑3流量的花费为9w,可以这样建立,直接将容量c的边拆成c条容量为1的边,每条边的费用不一样才能满足要求,费用分别为为w,3w,5w.....,这样的话就会使得满足费用与流量的平方成正比。
代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
#define PI acos(-1.0)
const int maxn=1e3+100,maxm=1e5+100,inf=0x3f3f3f3f,mod=1e9+7;
const ll INF=1e13+7;
struct edge
{
    int from,to;
    ll c,w;
};
int n;
vector<edge>es;
vector<int>G[maxn];
ll dist[maxn];
int pre[maxn];
inline void addedge(int u,int v,ll c,ll w)
{
    es.push_back((edge)
    {
        u,v,c,w
    });
    es.push_back((edge)
    {
        v,u,0,-w
    });
    int x=es.size();
    G[u].push_back(x-2);
    G[v].push_back(x-1);
}

bool spfa(int s,int t)
{
    static std::queue<int> q;
    static bool inq[maxn];
    for(int i=0; i<=n+10; i++) dist[i]=INF,inq[i]=false;
    pre[s]=-1;
    dist[s]=0;
    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=es[G[u][i]];
            if(e.c&&dist[e.to]>dist[u]+e.w)
            {
                pre[e.to]=G[u][i];
                dist[e.to]=dist[u]+e.w;
                if(!inq[e.to]) q.push(e.to),inq[e.to]=true;
            }
        }
    }
    return dist[t]<inf;
}

void dinic(int s,int t,ll f)
{
    ll flow=0,cost=0;
    while(spfa(s,t))
    {
        ll d=f;
        for(int i=t; i!=s; i=es[pre[i]].from)
            d=min(d,es[pre[i]].c);
        f-=d;
        flow+=d;
        cost+=d*dist[t];
        for(int i=t; i!=s; i=es[pre[i]].from)
        {
            es[pre[i]].c-=d;
            es[pre[i]^1].c+=d;
        }
        if(f<=0) break;
    }
    if(f) puts("-1");
    else printf("%lld\n",cost);
}

int main()
{
    int m;
    ll k;
    while(~scanf("%d%d%lld",&n,&m,&k))
    {
        for(int i=1; i<=m; i++)
        {
            int u,v;
            ll c,w;
            scanf("%d%d%lld%lld",&u,&v,&w,&c);
            for(ll t=1; t<=c; t++)
                addedge(u,v,1LL,(t*t-(t-1)*(t-1))*w);
        }
        dinic(1,n,k);
        es.clear();
        for(int i=0; i<=n+10; i++) G[i].clear();
    }
    return 0;
}
最小费用流

 

转载于:https://www.cnblogs.com/GeekZRF/p/7576569.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值