uva 1486 - Transportation(最小费用流)

本文介绍了一种解决特定运输问题的方法:通过最小费用最大流算法来计算从城市1到城市N运输K单位货物所需的最低成本。该算法考虑了道路的安全系数及货物运输上限,并通过拆边法将二次费用转化为线性费用。

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

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 roadi, there is a coefficient ai. If you want to carryx units of goods along this road, you should payai* x2 dollars to hire guards to protect your goods. And what's worse, for each roadi, there is an upper boundCi, which means that you cannot transport more thanCi 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$ \le$N$ \le$100, 1$ \le$M$ \le$5000, 0$ \le$K$ \le$100). Then M lines followed, each contains four integers(ui,vi, ai,Ci), indicating there is a directed road from cityui to vi, whose coefficient isai and upper bound isCi. (1$ \le$ui,vi$ \le$N, 0 <ai$ \le$100,Ci$ \le$5)

Output

Output one line for each test case, indicating the minimum cost. If it is impossible to transport all theK 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

首先,构图上要用到拆边法,把ax2这样的边,拆为a,3a,5a,7a这样的容量为1的边。然后此题是固定流量的,所以在求最小费用,增广时要检查一下,在flow+a>=k的时候只增广k - flow单位的流量,然后终止程序。

#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;

const int maxn = 100+10;
const int INF = 1000000000;

struct Edge{
    int from,to,cap,flow,cost;
};

struct MCMF {
  int n, m, s, t;
  vector<Edge> edges;
  vector<int> G[maxn];
  int inq[maxn];         // 是否在队列中
  int d[maxn];           // Bellman-Ford
  int p[maxn];           // 上一条弧
  int a[maxn];           // 可改进量

  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, 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 k,int s, int t, int &flow,int &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;//s-t不连通,失败退出
    
    if(flow + a[t] >= k){//判断流量是否达到了k
        cost += d[t] * (k-flow);
        flow = k;
        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 Mincost(int &can,int s, int t) {
    int flow = 0,cost = 0;
    while(BellmanFord(can,s, t,flow, cost));
    can = flow;
    return cost;
  }

};

MCMF g;

int main(){
    int n,m,k;
    while(scanf("%d%d%d",&n,&m,&k) != EOF){
        g.init(n);
        int sorce = 0,sink = n-1;
        for(int i = 0;i < m;i++){
            int from,to,a,cap;
            scanf("%d%d%d%d",&from,&to,&a,&cap);
            for(int i = 1;i <= cap;i++){
                g.AddEdge(from-1,to-1,1,a*(2*i-1));
            }
        }
        int can = k;
        int ans = g.Mincost(can,sorce,sink);
        if(can == k)    printf("%d\n",ans);
        else printf("-1\n");
    }
    return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值