题目

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点最少花的时间是多少?

这题感觉很不错。

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

代码

#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
#define INF (1<<30)
#define MAXN 2555
#define MAXM 11111
struct Edge{
int u,v,cap,cost,next;
}edge[MAXM];
int head[MAXN];
int NV,NE,vs,vt; void addEdge(int u,int v,int cap,int cost){
edge[NE].u=u; edge[NE].v=v; edge[NE].cap=cap; edge[NE].cost=cost;
edge[NE].next=head[u]; head[u]=NE++;
edge[NE].u=v; edge[NE].v=u; edge[NE].cap=0; edge[NE].cost=-cost;
edge[NE].next=head[v]; head[v]=NE++;
}
bool vis[MAXN];
int d[MAXN],pre[MAXN];
bool SPFA(){
for(int i=0;i<NV;++i){
vis[i]=0;
d[i]=INF;
}
vis[vs]=1;
d[vs]=0;
queue<int> que;
que.push(vs);
while(!que.empty()){
int u=que.front(); que.pop();
for(int i=head[u]; i!=-1; i=edge[i].next){
int v=edge[i].v;
if(edge[i].cap && d[v]>d[u]+edge[i].cost){
d[v]=d[u]+edge[i].cost;
pre[v]=i;
if(!vis[v]){
vis[v]=1;
que.push(v);
}
}
}
vis[u]=0;
}
return d[vt]!=INF;
}
int MCMF(int tot){
int lastSum=0,lastTime=0;
while(SPFA()){
int flow=INF,cost=0;
for(int u=vt; u!=vs; u=edge[pre[u]].u){
flow=min(flow,edge[pre[u]].cap);
}
for(int u=vt; u!=vs; u=edge[pre[u]].u){
edge[pre[u]].cap-=flow;
edge[pre[u]^1].cap+=flow;
cost+=edge[pre[u]].cost;
} int time2=cost-lastTime,time1;
if(lastSum){
time1=tot/lastSum+(tot%lastSum!=0);
if(time1<=time2){
return lastTime+time1;
}
}
tot-=time2*lastSum;
tot-=flow;
if(tot<=0){
return cost;
}
lastTime=cost;
lastSum+=flow;
}
if(lastSum==0) return -1;
return lastTime+tot/lastSum+(tot%lastSum!=0);
} int main(){
int n,m,k;
while(~scanf("%d%d%d",&n,&m,&k)){
vs=0; vt=n-1; NV=n; NE=0;
memset(head,-1,sizeof(head));
int a,b,c;
while(m--){
scanf("%d%d%d",&a,&b,&c);
addEdge(a,b,c,1);
}
if(k==0){
puts("0");
continue;
}
int ans=MCMF(k);
if(ans==-1) puts("No solution");
else printf("%d\n",ans);
}
return 0;
}

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 Service 与 Thread 的区别

    Ref:http://blog.csdn.net/jiangwei0910410003/article/details/17008687 1). Thread:Thread 是程序执行的最小单元,它是 ...

  2. Java返回距离当前时间段

    /** * 计算该时间离当前时间的差距 * @param time 格式为:yyyy-MM-dd HH:mm:ss * @return */ public static String getShort ...

  3. NYOJ题目822画图

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAtUAAAHzCAIAAABgzHaKAAAgAElEQVR4nO3dPVLjzBoG0LsJ514IsR

  4. hdu 1860统计字符

    本来是想用map写的,但是map里面会自动按字典序升序排序导致wa了一把,供 #include<time.h> #include <cstdio> #include <i ...

  5. 关于JAVA的String类的一些方法

    一.得到字符串对象的有关信息 1.通过调用length()方法得到String的长度. String str=”This is a String”; int len =str.length(); 2. ...

  6. jq 轮播图

    <style> #focus{width:500px;height:200px;overflow:hidden;/*用一个div把图片包含设置超出范围隐藏*/} </style> ...

  7. 18.中介者模式(Mediator Pattern)

    using System; namespace Test { class Program { /// <summary> /// 中介者模式,定义了一个中介对象来封装一系列对象之间的交互关 ...

  8. Codeforces Round #363 Fix a Tree(树 拓扑排序)

    先做拓扑排序,再bfs处理 #include<cstdio> #include<iostream> #include<cstdlib> #include<cs ...

  9. android 入门-Eclipse 费解的问题

    1.第一次打开eclipse的时候 代码程序出好多红点.等待加载项目,如果加载完项目之后仍然存在,请重启eclipse. 2.如果你在创建页面中的button 的时候,设置了android:gravi ...

  10. 笔记本win7共享WIFI

    创建无线网络 (1)netsh wlan set hostednetwork mode=allow ssid=网络名 key=密码 启动承载网络(2)netsh wlan start hostedne ...