HDU4807 Lunch Time(费用流变种)
题目
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(费用流变种)的更多相关文章
- Lunch Time(费用流变型题,以时间为费用)
Lunch Time http://acm.hdu.edu.cn/showproblem.php?pid=4807 Time Limit: 4000/2000 MS (Java/Others) ...
- hdu4807枚举费用流
题意: 给你一个有向图,每条边上都有每一时刻的最大流量,有k个人在点0,他们要去点n-1,问你最晚到达的那个人最快要多久. 思路: 这个题目做了很多次,用过费用流,也用过最大流,结 ...
- hdu-5988 Coding Contest(费用流)
题目链接: Coding Contest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Ot ...
- Coding Contest(费用流变形题,double)
Coding Contest http://acm.hdu.edu.cn/showproblem.php?pid=5988 Time Limit: 2000/1000 MS (Java/Others) ...
- 2016青岛区域赛.Coding Contest(费用流 + 概率计算转换为加法计算)
Coding Contest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
- 【PowerOJ1751&网络流24题】数字梯形问题(费用流)
题意: 思路: [问题分析] 求图的最大权不相交路径及其变种,用费用最大流解决. [建模方法] 规则(1) 把梯形中每个位置抽象为两个点<i.a>,<i.b>,建立附加源S汇T ...
- POJ2195 Going Home[费用流|二分图最大权匹配]
Going Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22088 Accepted: 11155 Desc ...
- BZOJ3130: [Sdoi2013]费用流[最大流 实数二分]
3130: [Sdoi2013]费用流 Time Limit: 10 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 960 Solved: 5 ...
- 洛谷 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 ...
随机推荐
- Jams倒酒
Jams是一家酒吧的老板,他的酒吧提供2种体积的啤酒,a ml 和 b ml,分别使用容积为a ml 和 b ml的酒杯来装载. 酒吧的生意并不好.Jams发现酒鬼们都很穷,不像他那么土豪.有时,他们 ...
- SQLServer自定义函数简单演示
CREATE FUNCTION [ schema_name. ] function_name ( [ { @parameter_name [ AS ][ type_schema_name. ] par ...
- Swift - 键盘弹出样式
Swift提供了11种键盘类型: 在开发中,我们可以根据不同的需求,选择不同的键盘样式,例如,当我们只需要输入手机号码时,可以选择纯数字类型的键盘(.NumbersAndPunctuation),当我 ...
- php 分页
分页类 <?php /** file: page.class.php 完美分页类 Page */ class Page { private $total; //数据表中总记录数 private ...
- 启动ip转法功能
这种方法无需重启: [root@ha02 ~]# cat /proc/sys/net/ipv4/ip_forward [root@ha02 ~]# sysctl -w net.ipv4.ip_forw ...
- Jquery.Datatables 结合时间段查询,daterangepicker实现Datatables表格带参数查询
参考:http://datatables.club/example/user_share/send_extra_param.html 下载地址:http://pan.baidu.com/s/1 ...
- Tensorflow 的Word2vec demo解析
简单demo的代码路径在tensorflow\tensorflow\g3doc\tutorials\word2vec\word2vec_basic.py Sikp gram方式的model思路 htt ...
- ASP.NET MVC 伪静态的实现
public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.Ignore ...
- 【翻译十七】java-并发之高性能对象
High Level Concurrency Objects So far, this lesson has focused on the low-level APIs that have been ...
- OCJP(1Z0-851) 模拟题分析(三)over
Exam : 1Z0-851 Java Standard Edition 6 Programmer Certified Professional Exam 以下分析全都是我自己分析或者参考网上的,定有 ...