您的位置:首页 > 理论基础 > 计算机网络

(beginer) 网络流(最小费用流+费用跟流量有关) UVA 1486 - Transportation

2014-02-08 09:55 471 查看
There are N cities, and M directed roads connecting them. Now you want to transportK 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 pay
ai* x2 dollars to hire guards to protect your goods. And what's worse, for each roadi, there is an upper bound
Ci, 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

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 cityui to
vi, whose coefficient isai 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 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


题意:你需要从城市1运送K个货物到N城市,每条路有一定危险系数,所以你要顾保镖,费用为危险系数*货物数量。而且每条路最多只能运5个货物。问能不能任务能不能完成,如果能完成最少需要多少钱。

思路:因为cost=a*x^2 , 所有我们要把他拆成多条边。为什么可行?因为我们的货物单位都是整数所以费用只可能为a , 4*a , 9a,.....那么我们只需要把边拆成费用分别为 a , 3*a , 5*a , 7*a .... 那么当容量为1时,当然走费用最小的a,如果容量为2时,当然走费用最小的a和3a,以此类推,实际效果就是a*x^2,建好图后套个最小费用最大流的模板就行了,看最大流是不是K,不是就输出-1,否则输出花费。

代码;
#include<iostream>
#include<cstring>
#include<string.h>
#include<vector>
#include<queue>
using namespace std;
const int maxn = 110;
const int inf = 1e9;
int n , m , k;
int d[maxn] , a[maxn];
bool inq[maxn];
int p[maxn];

struct Edge
{
int u , v , cost;
int flow , cap;
Edge(int uu,int vv,int cost,int flow,int cap)
: u(uu) , v(vv) , cost(cost) , flow(flow) , cap(cap) { }
};

vector<Edge> edge;
vector<int> G[maxn];
void add(int s,int t,int cap,int cost)
{
edge.push_back(Edge(s,t,cost,0,cap));
edge.push_back(Edge(t,s,-cost,0,0));
int x = edge.size();
G[s].push_back(x-2);
G[t].push_back(x-1);
}

int cap[] = { 0,1,3,5,7,9 };
void input()
{
edge.clear();
for (int i = 0 ; i < n+1 ; ++i) G[i].clear();
add(0,1,k,0);
int u , v , a , c;
while (m--) {
scanf("%d%d%d%d",&u,&v,&a,&c);
for (int i = 1 ; i <= c ; ++i)
add(u,v,1,a*cap[i]);
}
}

bool spfa(int s,int t,int &flow,int &cost)
{
memset(inq,false,sizeof(inq));
for (int i = 0 ; i <= n ; ++i) d[i] = a[i] = inf;
queue<int> q;
q.push(s);
d[s] = 0; inq[s] = false;
while (q.size()) {
int u = q.front(); q.pop();
inq[u] = false;
for (int i = 0 ; i < G[u].size() ; ++i) {
Edge & e = edge[G[u][i]];
if (e.cap > e.flow && d[e.v] > d[u]+e.cost) {
d[e.v] = d[u] + e.cost;
p[e.v] = G[u][i];
a[e.v] = min(a[u],e.cap-e.flow);
if (!inq[e.v]) { inq[e.v] = true; q.push(e.v); }
}
}
}
if (d[t]==inf) return false;
flow += a[t];
cost += a[t]*d[t];
int x = t;
while (x!=s) {
edge[p[x]].flow += a[t];
edge[p[x]^1].flow -= a[t];
x = edge[p[x]].u;
}
return true;
}

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

int main()
{
while (scanf("%d%d%d",&n,&m,&k)==3)
{
input();
printf("%d\n",mincost(0,n));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: