HDU 3667Transportation(最小费用最大流)拆边,经典
2015-06-27 21:47
267 查看
Transportation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2348 Accepted Submission(s): 1000
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
题意:
用 0 到 n-1 运送k 个货物,m条边,每条边,u,v,a,c,; 每条边表示 运送 x 单元货物的花费为 a * x*x,c 表示最大流量 ,
求 最小费用 ,若不能全部运送输出 -1;
题解: 拆边 ,看到这个题直接的思路就是费用流,一开始提了个模版 上去 发现不对,我们以前做的 花费是 a*f ,而现在是 a*f*f(此时 以 最短路 已不能找到正确的答案);
所以我们要变为 a*f ,那么就是,我们 发现 第一次运送时
费用为a,第二次取这条路时费用为3a(即流量为2时费用值为a+3a=4a),……,第i次取这条路时费用为(2*i-1)*a
在 u 到 u 连 c 条边,每条边的流量 为 1 ,费用 (2*i - 1)*a
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2348 Accepted Submission(s): 1000
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
题意:
用 0 到 n-1 运送k 个货物,m条边,每条边,u,v,a,c,; 每条边表示 运送 x 单元货物的花费为 a * x*x,c 表示最大流量 ,
求 最小费用 ,若不能全部运送输出 -1;
题解: 拆边 ,看到这个题直接的思路就是费用流,一开始提了个模版 上去 发现不对,我们以前做的 花费是 a*f ,而现在是 a*f*f(此时 以 最短路 已不能找到正确的答案);
所以我们要变为 a*f ,那么就是,我们 发现 第一次运送时
费用为a,第二次取这条路时费用为3a(即流量为2时费用值为a+3a=4a),……,第i次取这条路时费用为(2*i-1)*a
在 u 到 u 连 c 条边,每条边的流量 为 1 ,费用 (2*i - 1)*a
#include<stdio.h> #include<string.h> #include<queue> using namespace std; const int MAXN = 100010; const int MAXM = 100100; const int INF = 1<<30; struct EDG{ int to,next,cap,flow; int cost; //单价 }edg[MAXM]; int head[MAXN],eid; int pre[MAXN], cost[MAXN] ; //点0~(n-1) void init(){ eid=0; memset(head,-1,sizeof(head)); } inline void addEdg(int u,int v,int cap,int cst){ edg[eid].to=v; edg[eid].next=head[u]; edg[eid].cost = cst; edg[eid].cap=cap; edg[eid].flow=0; head[u]=eid++; edg[eid].to=u; edg[eid].next=head[v]; edg[eid].cost = -cst; edg[eid].cap=0; edg[eid].flow=0; head[v]=eid++; } bool inq[MAXN]; int q[MAXN]; inline bool spfa(int sNode,int eNode,int n){ int l=0 , r=0; for(int i=0; i<n; i++){ inq[i]=false; cost[i]= INF; } cost[sNode]=0; inq[sNode]=1; pre[sNode]=-1; q[r++]=sNode; while(l!=r){ int u=q[l++]; if(l==MAXN)l=0; inq[u]=0; for(int i=head[u]; i!=-1; i=edg[i].next){ int v=edg[i].to; if(edg[i].cap-edg[i].flow>0 && cost[v]>cost[u]+edg[i].cost){ //在满足可增流的情况下,最小花费 cost[v] = cost[u]+edg[i].cost; pre[v]=i; //记录路径上的边 if(!inq[v]){ if(r==MAXN)r=0; q[r++]=v; inq[v]=1; } } } } return cost[eNode]!=INF; //判断有没有增广路 } //反回的是最大流,最小花费为minCost int minCost_maxFlow(int sNode,int eNode ,int& minCost,int n){ int ans=0; while(spfa(sNode,eNode,n)){ ans++; minCost+=cost[eNode]; int i=pre[eNode]; while(i!=-1){ edg[i].flow+=1; edg[i^1].flow-=1; i=pre[edg[i^1].to]; } } return ans; } inline void scanf(int& num){ char ch; while(ch=getchar()){ if(ch>='0'&&ch<='9')break; } num=ch-'0'; while(ch=getchar()){ if(ch<'0'||ch>'9')break; num=num*10+ch-'0'; } } int main(){ int n,m,k; while(scanf("%d%d%d",&n,&m,&k)>0){ init(); int s=0,t=n; addEdg(s , 1 , k , 0); while(m--){ int u,v,a,c; scanf(u); scanf(v); scanf(a); scanf(c); for(int i=1; i<=c; i++) addEdg(u,v,1,a*(2*i-1)); } int mincost=0; k-=minCost_maxFlow(s,t,mincost,t+1); if(k!=0) mincost=-1; printf("%d\n",mincost); } }
相关文章推荐
- 【转】win7 64位配置bundler-v0.4-source
- 《一个程序猿的生命周期》有感
- java学习01--Java概述 、基础
- poj 1004
- 认识自己——恐惧的奴隶5:哥哥
- 图像扩展函数cvCopyMakeBorder的使用
- 杭电 HDU ACM 1166 敌兵布阵(树状数组)
- rt5350 捕获sn9c291 ov9712 模块jpeg图片效果
- C#的发展已经15年了 。。。历史发展
- Java初涉之2--父类和子类的类型转换
- Java学习日记之for while do...while
- MFC六大机制之一:程序起动机制
- HDU_2082 找单词(生成函数)
- poj 1003
- vs2010如何检测内存泄漏
- HTTP传输层异常处理办法及测试总结
- A Simple OpenGL Shader Example
- HDU 1003 maxsum
- 前端后台学习笔记汇杂
- POJ 1195 Mobile phones【 二维树状数组 】