HDU 3667 Transportation(最小费用流 拆边)

本文介绍了一种解决特定运输问题的方法——最小成本流算法。该问题涉及在存在费用和容量限制的有向图中,从起点到终点运送指定数量的商品,同时使总费用最小。文章通过拆边技巧将原问题转化为标准的最小成本流问题,并提供了完整的C++代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Transportation

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


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 ai. If you want to carry x units of goods along this road, you should pay ai * x2 dollars to hire guards to protect your goods. And what’s worse, for each road i, there is an upper bound Ci, which means that you cannot transport more than Ci 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 (ui, vi, ai, Ci), indicating there is a directed road from city ui to vi, whose coefficient is ai and upper bound is Ci. (1 <= ui, vi <= N, 0 < ai <= 100, Ci <= 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
题解:
拆边,若最大流量为5,则拆成5条边,每条边的流量都为1,第一条边单位费用为1*a,第二条为3*a,
第三条5*a, ..7*a..9*a
#include<bits/stdc++.h>
using namespace std;
const int maxn=105;
const int inf=1e9;
typedef pair<int,int>P;
int h[maxn],dist[maxn],prevv[maxn],preve[maxn];
struct node
{
    int to,cap,cost,rev;
};
vector<node>G[maxn];
void add(int from,int to,int cost,int cap)
{
    G[from].push_back((node){to,cap,cost,G[to].size()});
    G[to].push_back((node){from,0,-cost,G[from].size()-1});
}
int min_cost_flow(int s,int t,int f)
{
    int res=0;
    fill(h,h+maxn,0);
    while(f>0)
    {
        priority_queue<P,vector<P>,greater<P> >que;
        fill(dist,dist+maxn,inf);
        dist[s]=0;que.push(P(0,s));
        while(!que.empty())
        {
            P p=que.top();que.pop();
            int v=p.second;
            if(dist[v]<p.first)continue;
            for(int i=0;i<G[v].size();i++)
            {
                node &e=G[v][i];
                if(e.cap>0&&dist[e.to]>dist[v]+e.cost+h[v]-h[e.to])
                {
                    dist[e.to]=dist[v]+e.cost+h[v]-h[e.to];
                    prevv[e.to]=v;
                    preve[e.to]=i;
                    que.push(P(dist[e.to],e.to));
                }
            }
        }
        if(dist[t]==inf)return -1;
        for(int i=1;i<=t;i++)h[i]+=dist[i];
        int d=f;
        for(int v=t;v!=s;v=prevv[v])d=min(d,G[prevv[v]][preve[v]].cap);
        f-=d; res+=d*h[t];
        for(int v=t;v!=s;v=prevv[v])
        {
            node &e=G[prevv[v]][preve[v]];
            e.cap-=d;
            G[v][e.rev].cap+=d;
        }
    }
    return res;
}
int main()
{
    int n,m,k;
    while(~scanf("%d%d%d",&n,&m,&k))
    {
        for(int i=0;i<maxn;i++)G[i].clear();
        for(int i=1;i<=m;i++)
        {
            int a,b,c,d;scanf("%d%d%d%d",&a,&b,&c,&d);
            for(int i=1;i<=d;i++)
            {
                add(a,b,(i*2-1)*c,1);
            }
        }
        printf("%d\n",min_cost_flow(1,n,k));
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值