描述

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. POJ P1185 炮兵阵地 【状压dp】

    炮兵阵地 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 29502 Accepted: 11424 Description 司令 ...

  2. HDU4625 JZPTREE——第二类斯特林数

    复杂度大概O(nk) 一些尝试:1.对每个点推出1,2,3,,,到k次方的值.但是临项递推二项式展开也要考虑到具体每个点的dist 2.相邻k次方递推呢?递推还是不能避免k次方的展开 k次方比较讨厌, ...

  3. Java国密相关算法(bouncycastle)

    公用类算法: PCIKeyPair.java /** * @Author: dzy * @Date: 2018/9/27 14:18 * @Describe: 公私钥对 */ @Data @AllAr ...

  4. laravel 5.1 单元测试 Cannot modify header information 错误

    运行phpunit的时候加上参数 --stderr ./vendor/bin/phpunit --stderr

  5. laravel5.5 不能正常自动回复的问题

    虽然开启了APP_DEBUG 但是 log 却没有记录任何错误信息,后来经过测试发现原来是路由问题,因为微信服务器发送消息是使用 post 方法,但是我的路由定义只定义了 get (tp 用多了习惯了 ...

  6. 问题分析 - 电容的ESR

    ESR,是Equivalent Series Resistance三个单词的缩写,翻译过来就是“等效串连电阻” 理论上,一个完美的电容,自身不会产生任何能量损失,但是实际上,因为制造电容的材料有电阻, ...

  7. python基础5--模块

    模块 一.模块简介 模块是一个包含有定义的函数和变量的文件,其后缀名是.py.Python的强大之处在于他有非常丰富和强大的标准库和第三方库,几乎你想实现的任何功能都有相应的Python库支持. 标准 ...

  8. Centos7中使用ipset

      1.禁用firewalld systemctl stop firewalld systemctl disable firewalld   2.安装ipset yum -y install ipse ...

  9. Python基础之面向对象(进阶篇)

    面向对象是一种编程方式,此编程方式的实现是基于对 类 和 对象 的使用 类 是一个模板,模板中包装了多个“函数”供使用(可以讲多函数中公用的变量封装到对象中) 对象,根据模板创建的实例(即:对象),实 ...

  10. Jquery中find与each方法使用详解

    本文实例讲述了jQuery中find与each方法用法.分享给大家供大家参考.具体如下: 一.find()方法 jquery选择器非常强大,利用css的命名规约,可以更快更方便的找出想要的元素. 图解 ...