Transportation

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

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 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


比较有意思的题目。不过还是很容易看出来是网络流,并且是费用流。

考虑建图:因为这个费用并不是单独的线性关系,我们不能之间建图。

所以我们要把边拆成C条,C为边的最大流量。然后每条边的流量分别为:

a,i * i-(i-1) * (i-1) *a ,具体原因,自己可以想一下。

然后我们限流为K,新建立节点就行,不能通过流量大于等于K时break,因为流量可能会大于K。


AC代码:

#pragma GCC optimize(2)
#include<bits/stdc++.h>
//#define int long long
using namespace std;
const int inf=0x3f3f3f3f;
const int N=1e4+10,M=1e6+10;
int n,m,k,v[N],e[N],d[N],maxflow,res,s,t;
int head[N],nex[M],to[M],w[M],flow[M],tot;
inline void ade(int a,int b,int c,int d){
	to[++tot]=b; flow[tot]=c; w[tot]=d; nex[tot]=head[a]; head[a]=tot;
}
inline void add(int a,int b,int c,int d){
	ade(a,b,c,d);	ade(b,a,0,-d);
}
inline void init(){
	tot=1;	memset(head,0,sizeof head);	maxflow=res=0; s=0;	t=n+1;
	add(s,1,k,0);	add(n,t,k,0);
}
inline int spfa(){
	memset(d,inf,sizeof d); d[s]=0;	queue<int> q;	q.push(s);
	int vis[N]={0}; vis[s]=1;
	while(q.size()){
		int u=q.front();	q.pop();	vis[u]=0;
		for(int i=head[u];i;i=nex[i]){
			if(flow[i]&&d[to[i]]>d[u]+w[i]){
				d[to[i]]=d[u]+w[i];
				v[to[i]]=u; e[to[i]]=i;
				if(!vis[to[i]])	vis[to[i]]=1,q.push(to[i]);
			}
		}
	} 
	return d[t]!=inf;
}
void EK(){
	while(spfa()){
		int mi=inf;
		for(int i=t;i!=s;i=v[i])	mi=min(mi,flow[e[i]]);
		for(int i=t;i!=s;i=v[i])	flow[e[i]]-=mi,flow[e[i]^1]+=mi;
		maxflow+=mi; res+=d[t]*mi;
	}
}
signed main(){
	while(~scanf("%d %d %d",&n,&m,&k)){
		init();
		while(m--){
			int u,v,a,c;	scanf("%d %d %d %d",&u,&v,&a,&c);
			for(int i=1;i<=c;i++){
				add(u,v,1,a*(i*i-(i-1)*(i-1)));
			}
		}
		EK();
		printf("%d\n",maxflow==k?res:-1);
	}
	return 0;
}