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

来源:互联网 发布:linux du命令 求和 编辑:程序博客网 时间:2024/05/16 05:12

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 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 21 2 1 22 1 21 2 1 12 2 21 2 1 21 2 2 2
 

Sample Output
4-13
 

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 01 2 1 2*/


0 0
原创粉丝点击