题目

Source

http://acm.hdu.edu.cn/showproblem.php?pid=4807

Description

The campus of Nanjing University of Science and Technology can be viewed as a graph with N vertexes and M directed edges (vertexes are numbered from 0 to N - 1). Each edge has the same length 1. Every day, there are K students walking to the dinning-hall (vertex N - 1) from the teaching building (vertex 0) at lunch time. They all want reach the dinning-hall as soon as possible. However, each edge can only serve at most ci students at any time. Can you make arrangements for students, so that the last student can reach the dinning-hall as soon as possible? (It is assumed that the speed of the students is 1 edge per unit time)

Input

There are several test cases, please process till EOF.
The first line of each test case contains three integer N(2 <= N <= 2500), M(0 <= M <= 5000), K(0 <= K <= 109). Then follows M lines, each line has three numbers ai, bi, ci(0 <= ci <= 20), means there is an edge from vertex ai to bi with the capacity ci.

Output

For each test case, print an integer represents the minimum time. If the requirements can not be met, print “No solution”(without quotes) instead.

Sample Input

5 6 4
0 1 2
0 3 1
1 2 1
2 3 1
1 4 1
3 4 2
3 3 10
0 1 1
1 2 1
0 2 1
2 0 1

Sample Output

3
6
No solution

分析

题目大概说一张有向图,同一时间边只能容量一定数量的人,k个人从0点出发,移动到下一点花费1时间,问所有人都到达n-1点最少花的时间是多少?

这题感觉很不错。

  • 跑费用流,找可以找到若干条连续最短增广路,这些最短路叠加在一起是满足容量限制条件的,或者简单说这些路径是不重合的。
  • 而对于这题,这若干条路,就相当于若干条并行的可以同时从起点出发到达终点的路径,每条路都有各自走完的时间(费用)和每次走完到达的人(流量)。
  • 或者更清晰点,这若干条路就是若干条流水线!流水线满流后,每隔一单位时间完工的物品就是流水线最大流量。所以跑费用流找到若干条路,然后模拟一下流水线即可。。

代码

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<queue>
  4. #include<algorithm>
  5. using namespace std;
  6. #define INF (1<<30)
  7. #define MAXN 2555
  8. #define MAXM 11111
  9. struct Edge{
  10. int u,v,cap,cost,next;
  11. }edge[MAXM];
  12. int head[MAXN];
  13. int NV,NE,vs,vt;
  14.  
  15. void addEdge(int u,int v,int cap,int cost){
  16. edge[NE].u=u; edge[NE].v=v; edge[NE].cap=cap; edge[NE].cost=cost;
  17. edge[NE].next=head[u]; head[u]=NE++;
  18. edge[NE].u=v; edge[NE].v=u; edge[NE].cap=0; edge[NE].cost=-cost;
  19. edge[NE].next=head[v]; head[v]=NE++;
  20. }
  21. bool vis[MAXN];
  22. int d[MAXN],pre[MAXN];
  23. bool SPFA(){
  24. for(int i=0;i<NV;++i){
  25. vis[i]=0;
  26. d[i]=INF;
  27. }
  28. vis[vs]=1;
  29. d[vs]=0;
  30. queue<int> que;
  31. que.push(vs);
  32. while(!que.empty()){
  33. int u=que.front(); que.pop();
  34. for(int i=head[u]; i!=-1; i=edge[i].next){
  35. int v=edge[i].v;
  36. if(edge[i].cap && d[v]>d[u]+edge[i].cost){
  37. d[v]=d[u]+edge[i].cost;
  38. pre[v]=i;
  39. if(!vis[v]){
  40. vis[v]=1;
  41. que.push(v);
  42. }
  43. }
  44. }
  45. vis[u]=0;
  46. }
  47. return d[vt]!=INF;
  48. }
  49. int MCMF(int tot){
  50. int lastSum=0,lastTime=0;
  51. while(SPFA()){
  52. int flow=INF,cost=0;
  53. for(int u=vt; u!=vs; u=edge[pre[u]].u){
  54. flow=min(flow,edge[pre[u]].cap);
  55. }
  56. for(int u=vt; u!=vs; u=edge[pre[u]].u){
  57. edge[pre[u]].cap-=flow;
  58. edge[pre[u]^1].cap+=flow;
  59. cost+=edge[pre[u]].cost;
  60. }
  61.  
  62. int time2=cost-lastTime,time1;
  63. if(lastSum){
  64. time1=tot/lastSum+(tot%lastSum!=0);
  65. if(time1<=time2){
  66. return lastTime+time1;
  67. }
  68. }
  69. tot-=time2*lastSum;
  70. tot-=flow;
  71. if(tot<=0){
  72. return cost;
  73. }
  74. lastTime=cost;
  75. lastSum+=flow;
  76. }
  77. if(lastSum==0) return -1;
  78. return lastTime+tot/lastSum+(tot%lastSum!=0);
  79. }
  80.  
  81. int main(){
  82. int n,m,k;
  83. while(~scanf("%d%d%d",&n,&m,&k)){
  84. vs=0; vt=n-1; NV=n; NE=0;
  85. memset(head,-1,sizeof(head));
  86. int a,b,c;
  87. while(m--){
  88. scanf("%d%d%d",&a,&b,&c);
  89. addEdge(a,b,c,1);
  90. }
  91. if(k==0){
  92. puts("0");
  93. continue;
  94. }
  95. int ans=MCMF(k);
  96. if(ans==-1) puts("No solution");
  97. else printf("%d\n",ans);
  98. }
  99. return 0;
  100. }

HDU4807 Lunch Time(费用流变种)的更多相关文章

  1. Lunch Time(费用流变型题,以时间为费用)

    Lunch Time http://acm.hdu.edu.cn/showproblem.php?pid=4807 Time Limit: 4000/2000 MS (Java/Others)     ...

  2. hdu4807枚举费用流

    题意:      给你一个有向图,每条边上都有每一时刻的最大流量,有k个人在点0,他们要去点n-1,问你最晚到达的那个人最快要多久. 思路:      这个题目做了很多次,用过费用流,也用过最大流,结 ...

  3. hdu-5988 Coding Contest(费用流)

    题目链接: Coding Contest Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Ot ...

  4. Coding Contest(费用流变形题,double)

    Coding Contest http://acm.hdu.edu.cn/showproblem.php?pid=5988 Time Limit: 2000/1000 MS (Java/Others) ...

  5. 2016青岛区域赛.Coding Contest(费用流 + 概率计算转换为加法计算)

    Coding Contest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  6. 【PowerOJ1751&网络流24题】数字梯形问题(费用流)

    题意: 思路: [问题分析] 求图的最大权不相交路径及其变种,用费用最大流解决. [建模方法] 规则(1) 把梯形中每个位置抽象为两个点<i.a>,<i.b>,建立附加源S汇T ...

  7. POJ2195 Going Home[费用流|二分图最大权匹配]

    Going Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22088   Accepted: 11155 Desc ...

  8. BZOJ3130: [Sdoi2013]费用流[最大流 实数二分]

    3130: [Sdoi2013]费用流 Time Limit: 10 Sec  Memory Limit: 128 MBSec  Special JudgeSubmit: 960  Solved: 5 ...

  9. 洛谷 1004 dp或最大费用流

    思路: dp方法: 设dp[i][j][k][l]为两条没有交叉的路径分别走到(i,j)和(k,l)处最大价值. 则转移方程为 dp[i][j][k][l]=max(dp[i-1][j][k-1][l ...

随机推荐

  1. [Android Pro] http请求中传输base64出现加号变空格的解决办法

    reference to : http://blog.csdn.net/jsjwbxzy/article/details/45970231 try { des = URLEncoder.encode( ...

  2. IIS服务的部署

    1.安装 C:\Windows\Microsoft.NET\Framework\v4.0.30319 aspnet_regiis -i2.添加应用程序,选择Asp.net4.03.应用目录 IIS_U ...

  3. eclipse 中添加工程 Some projects cannot be imported because they already exist in the workspace

    第一次从外部文件导入HelloWorld工程到workspace目录中,成功. 删除后,再次从外部导入workspace目录提示 Some projects cannot be imported be ...

  4. MVC学习笔记---各种上下文context

    0  前言 AspNet MVC中比较重要的上下文,有如下: 核心的上下文有HttpContext(请求上下文),ControllerContext(控制器上下文) 过滤器有关有五个的上下文Actio ...

  5. MVC缓存01,使用控制器缓存或数据层缓存

    对一些浏览频次多.数据量大的数据,使用缓存会比较好,而对一些浏览频次低,或内容因用户不同的,不太适合使用缓存.   在控制器层面,MVC为我们提供了OutputCacheAttribute特性:在数据 ...

  6. jQuery插件:跨浏览器复制jQuery-zclip(转载)

    转载地址:http://www.cnblogs.com/linjiqin/p/3532451.html jQuery-zclip是一个复制内容到剪贴板的jQuery插件,使用它我们不用考虑不同浏览器和 ...

  7. 在python多进程中使用manager和Barrier

    注意:Barrier是PYTHON3才有的功能,在2中无法测试. #!/usr/bin/env python # -*- coding: utf-8 -*- import multiprocessin ...

  8. 学习ASP.NET缓存机制

    缓存是大型BS架构网站的性能优化通用手段,之前知道有这个概念,并且也知道很重要,但是一直没静下心来了解.这次借着学习PetShop源码的机会熟悉一下ASP.NET基本的缓存机制(生产环境中的真实缓存有 ...

  9. 运维自动化之ansible的安装与使用(包括模块与playbook使用)(转发)

    原文  http://dl528888.blog.51cto.com/2382721/1435415 我使用过puppet(地址是http://dl528888.blog.51cto.com/2382 ...

  10. winows下使用ssh服务远程登录vbox中的虚拟机

    1.到http://www.putty.org/下载并安装SSH客户端 2.查看是否安装ssh服务 在ubuntu终端命令界面键入: #ssh localhost 如果出现下面提示则表示还没有安装: ...