杭电 HDU ACM Transportation (哈理工练习赛 费用流拆边)

Transportation

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


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
2010 Asia Regional Harbin


题意:有k个单位的物品要运输,还给了n个城市,m条路,每条路上有物品最高运输流量值,并且运费和在这条路上的流量有关,并给定每条路的费用系数a ,每条路上的费用和流量是a * x * x的关系。
思路:
因为每条边允许流量不超过5。我们按照允许流量拆边,假设cap == 3 我们拆成三条cap == 1的边,每条边的费用值设为1a 、3a、5a、7a…………这样我们再求最小费用最大流的时候,在通过相同流量的情况下肯定要选择费用最小的, 比如流量为3的时候肯定沿最短路增广费用值为1a 3a 和5a的三条边,这和两点之间是一条cap == 3 费用为9a的路是完全等价的。这样我们在每条路流量限制的条件下研究最大流是否能够大于等于k值,即是否可以增广到k,便可判断是否能够运输,如果能的话在此过程中我们同时也保证了费用肯定是最小的。(毕竟我们是沿“最短路”增广的,每次增广大小为1)
AC:
/*=============================================================================
#
#      Author: liangshu - cbam 
#
#      QQ : 756029571 
#
#      School : 哈尔滨理工大学 
#
#      Last modified: 2015-09-06 14:30
#
#     Filename: hdu1015.cpp
#
#     Description: 
#        The people who are crazy enough to think they can change the world, are the ones who do ! 
=============================================================================*/
#

#include<iostream>
    #include<sstream>
    #include<algorithm>
    #include<cstdio>
    #include<string.h>
    #include<cctype>
    #include<string>
    #include<cmath>
    #include<vector>
    #include<stack>
    #include<queue>
    #include<map>
    #include<set>
    using namespace std;
    const int INF = 1000000;
    const int maxn  = 5500;
struct Edge
{
    int from, to, cap, flow, cost;
};
int n, m, s, t,k;
vector<Edge>edges;
vector<int>G[maxn];
int inq[maxn];
int d[maxn];
int p[maxn];
int a[maxn];
void init(int 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});
    int t = edges.size();
    G[from].push_back(t - 2);
    G[to].push_back(t - 1);
}

bool BellmanFord(int s, int t, int &flow, int &cost)
{
    for(int i = 1; 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;
     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;
     }
     if(flow >= k)
        return 0;
     return true;
}

int Mincost(int s, int t)
{
    int flow = 0;int  cost = 0;
    while(BellmanFord(s, t, flow, cost))
     {
     }
     if(flow < k)return -1;
    return cost;
}

int main()
{
  while(scanf("%d%d%d",&n, &m , &k) != EOF)
  {
      s = 1;
      t = n;
      init(n);
      for(int i = 0; i < m ;i++){
        int x, y, z, b;
        scanf("%d%d%d%d",&x, &y, &z, &b);
        for(int  j = 1; j <= b; j++)
        AddEdge(x, y, 1,z * (j * 2 - 1));
      }
     cout<<Mincost(s, t)<<endl;
  }
  return 0;
}
/*
2 1 0
1 2 1 2
*/



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值