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

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$ \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 is Ci.(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
题意:你需要从城市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));
	}
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值