描述

There is a funny car racing in a city with n junctions and m directed roads.
The funny part is: each road is open and closed periodically. Each road
is associate with two integers (a, b), that means the road will be open
for a seconds, then closed for b seconds, then open for a seconds... All
these start from the beginning of the race. You must enter a road when
it's open, and leave it before it's closed again.
Your goal is to drive from junction s and arrive at junction t as early
as possible. Note that you can wait at a junction even if all its
adjacent roads are closed.

输入

There
will be at most 30 test cases. The first line of each case contains
four integers n, m, s, t (1<=n<=300, 1<=m<=50,000,
1<=s,t<=n). Each of the next m lines contains five integers u, v,
a, b, t (1<=u,v<=n, 1<=a,b,t<=105), that means
there is a road starting from junction u ending with junction v. It's
open for a seconds, then closed for b seconds (and so on). The time
needed to pass this road, by your car, is t. No road connects the same
junction, but a pair of junctions could be connected by more than one
road.

输出

For each test case, print the shortest time, in seconds. It's always possible to arrive at t from s.

样例输入

3 2 1 3
1 2 5 6 3
2 3 7 7 6
3 2 1 3
1 2 5 6 3
2 3 9 5 6

样例输出

Case 1: 20
Case 2: 9

题目来源

湖南省第九届大学生程序设计竞赛

题解:将时间看成最短路的模型,上一层的时间已经是最优的解了,所以下一层在更新的结点也会是最优的,如果到达的当前边时间为 T,那么就要判断是否要在当前城市等待了。。

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int INF = ;
int n,m;
struct Edge{
int v,a,b,t,next;
}edge[];
int tot;
int head[];
void addEdge(int u,int v,int a,int b,int t,int &k){
edge[k].v = v,edge[k].a = a,edge[k].b = b,edge[k].t = t,edge[k].next = head[u],head[u]=k++;
}
void init(){
memset(head,-,sizeof(head));
tot = ;
}
int low[];
bool vis[];
int dijsktra(int s,int t){
memset(vis,false,sizeof(vis));
for(int i=;i<=n;i++){
low[i] = INF;
}
low[s] = ;
vis[s] = true;
for(int i=;i<n;i++){
int MIN = INF;
for(int j=;j<=n;j++){
if(low[j]<MIN&&!vis[j]){
MIN = low[j];
s = j;
}
}
vis[s] = true;
for(int k=head[s];k!=-;k=edge[k].next){
int v = edge[k].v,a=edge[k].a,b = edge[k].b,tim = edge[k].t ;
if(a>=tim){
int t0 = low[s]%(a+b);
if(t0+tim<=a) low[v] = min(low[s]+tim,low[v]);
else low[v] = min(low[s]+a+b-t0+tim,low[v]);
}
}
}
return low[t];
}
int main()
{
int s,t,cas=;
while(scanf("%d%d%d%d",&n,&m,&s,&t)!=EOF){
init();
for(int i=;i<=m;i++){
int u,v,a,b,t;
scanf("%d%d%d%d%d",&u,&v,&a,&b,&t);
addEdge(u,v,a,b,t,tot);
}
printf("Case %d: %d\n",cas++,dijsktra(s,t));
}
return ;
}

Funny Car Racing(最短路变形)的更多相关文章

  1. POJ-2253.Frogger.(求每条路径中最大值的最小值,最短路变形)

    做到了这个题,感觉网上的博客是真的水,只有kuangbin大神一句话就点醒了我,所以我写这篇博客是为了让最短路的入门者尽快脱坑...... 本题思路:本题是最短路的变形,要求出最短路中的最大跳跃距离, ...

  2. POJ 3635 - Full Tank? - [最短路变形][手写二叉堆优化Dijkstra][配对堆优化Dijkstra]

    题目链接:http://poj.org/problem?id=3635 题意题解等均参考:POJ 3635 - Full Tank? - [最短路变形][优先队列优化Dijkstra]. 一些口胡: ...

  3. POJ 3635 - Full Tank? - [最短路变形][优先队列优化Dijkstra]

    题目链接:http://poj.org/problem?id=3635 Description After going through the receipts from your car trip ...

  4. POJ-1797Heavy Transportation,最短路变形,用dijkstra稍加修改就可以了;

    Heavy Transportation Time Limit: 3000MS   Memory Limit: 30000K          Description Background  Hugo ...

  5. HDOJ find the safest road 1596【最短路变形】

    find the safest road Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  6. HN0I2000最优乘车 (最短路变形)

    HN0I2000最优乘车 (最短路变形) 版权声明:本篇随笔版权归作者YJSheep(www.cnblogs.com/yangyaojia)所有,转载请保留原地址! [试题]为了简化城市公共汽车收费系 ...

  7. 天梯杯 PAT L2-001. 紧急救援 最短路变形

    作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图.在地图上显示有多个分散的城市和一些连接城市的快速道路.每个城市的救援队数量和每一条连接两个城市的快速道路长度都标在地图上.当其他城市有紧急求 ...

  8. Heavy Transportation POJ 1797 最短路变形

    Heavy Transportation POJ 1797 最短路变形 题意 原题链接 题意大体就是说在一个地图上,有n个城市,编号从1 2 3 ... n,m条路,每条路都有相应的承重能力,然后让你 ...

  9. POJ 2253 Frogger ( 最短路变形 || 最小生成树 )

    题意 : 给出二维平面上 N 个点,前两个点为起点和终点,问你从起点到终点的所有路径中拥有最短两点间距是多少. 分析 : ① 考虑最小生成树中 Kruskal 算法,在建树的过程中贪心的从最小的边一个 ...

随机推荐

  1. APIO2017

    商旅 在广阔的澳大利亚内陆地区长途跋涉后,你孤身一人带着一个背包来到了科巴.你被这个城市发达而美丽的市场所 深深吸引,决定定居于此,做一个商人.科巴有个集市,集市用从1到N的整数编号,集市之间通过M条 ...

  2. VSAM:视频监控系统 A System for Video Surveillance and Monitoring

    VSAM(VideoSurveillance and Monitoring)视频监控系统 Robotics Institute CMU 1:引言 2:试验床介绍 3:基本的视频分析算法:运动目标检测, ...

  3. [linux]linuxI/O测试的方法之dd

    参考http://www.thomas-krenn.com/en/wiki/Linux_I/O_Performance_Tests_using_dd Measuring Write Performan ...

  4. python 获取文件md5

    def GetFileMd5(filename): if not os.path.isfile(filename): return myhash = hashlib.md5() f = file(fi ...

  5. hdu 1907 John (anti—Nim)

    John Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)http://acm.h ...

  6. System Administrator(构造,图论)

    System Administrator time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  7. easyui 控件获取焦点方式

    针对easyui控件前端组织的dom做分析,如下: combo/combobox/combogrid类似结构如下: <input class="easyui-datebox dateb ...

  8. CSS 文本属性

    一.文本位置 text-align: 参数 /** * left center right:左中右**/text-align: left; 二.文本行间距 line-height: 参数 特别要注意: ...

  9. FastDFS图片服务器java后台的简单调用

    工具类: package com.liveyc.common.fdfs; import org.apache.commons.io.FilenameUtils; import org.csource. ...

  10. bzoj 2653 二分答案+可持久化线段树

    首先离散化,然后我们知道如果对于一个询问的区间[l1,r1],[l2,r2],我们二分到一个答案x,将[l1,r2]区间中的元素大于等于x的设为1,其余的设为-1,那么如果[l1,r1]的最大右区间和 ...