H - Funny Car Racing

Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Description

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.

Input

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.

Output

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

Sample Input

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

Sample Outpu

Case 1: 20

Case 2: 9

//这题的意思是:第一行4个整数 n,m,s,t . 1 < n < 300  , 1 < m < 50000 , 1 <= s , t <= n , n 是点的个数,m 是有向边的个数,s 是起点,t 是终点

第二行五个整数  u  , v  , a  , b  , t  ,说明有向边的起点,终点,这条边开启的时间,关闭的时间,通过需要的时间。这题简化了,通过这条边的条件是在关闭之前过去,不然就等到下个轮回再过去。显然t>a就不可能过去了,这数据应舍弃

//这题,是这个星期,完全没看别人的自己做出来的第一个题,对于刚学图论的有点难度。其实不难.

//spfa算法 0kb 50ms

 #include <cstdio>
#include <cstring>
#include <queue>
using namespace std; #define MAXN 500005
#define inf 0xfffffff struct Bian
{
int e;
int a,b,t;
int next;
}bian[MAXN]; int headlist[];
int d[];
int vis[];//其实我真不知道这个有什么用,不加也能过,有时候还能更快,但是每次看到spfa算法都加上我就加上了
int Case=; int check(int time,Bian x)//通过这条边需要的时间
{
int k=time%(x.a+x.b);
if (k+x.t<=x.a)
{
return x.t;
}
return x.a+x.b-k+x.t;
} void spfa(int n,int m,int star,int end)
{
queue<int> Q;
int i,x,v,y;
for (i=;i<=n;i++)
{
d[i]=inf;
vis[i]=;
}
d[star]=;
Q.push(star);
while (!Q.empty())
{
x=Q.front();
Q.pop();
vis[x]=;
for (i=headlist[x];i!=-;i=bian[i].next)
{
v=bian[i].e;
y=check(d[x],bian[i]);
if (d[v]>d[x]+y)
{
d[v]=d[x]+y;
if (!vis[v])
{
vis[v]=;
Q.push(v);
}
}
}
}
printf("Case %d: %d\n",++Case,d[end]);
} int main()
{
int n,m,s,e;
int u,v,a,b,t;
int i;
while (scanf("%d%d%d%d",&n,&m,&s,&e)!=EOF)
{
for (i=;i<=n;i++)
headlist[i]=-; for (i=;i<=m;i++)
{
scanf("%d%d%d%d%d",&u,&v,&a,&b,&t);
if (t>a) continue;
bian[i].e=v;
bian[i].a=a;
bian[i].b=b;
bian[i].t=t;
bian[i].next=headlist[u];//这叫邻接表吧
headlist[u]=i;
}
spfa(n,m,s,e);
}
return ;
}

H - Funny Car Racing的更多相关文章

  1. [POJ1801]Formula Racing(模拟)

    Formula Racing Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 289   Accepted: 77 Descr ...

  2. ACM: Racing Gems - 最长递增序列

    Racing Gems   You are playing a racing game.  Your character starts at the x axis (y = 0) and procee ...

  3. UVALive - 7374 Racing Gems 二维非递减子序列

    题目链接: http://acm.hust.edu.cn/vjudge/problem/356795 Racing Gems Time Limit: 3000MS 问题描述 You are playi ...

  4. Tian Ji -- The Horse Racing

    Tian Ji -- The Horse Racing Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Jav ...

  5. UVA 1344 Tian Ji -- The Horse Racing

    Tian Ji -- The Horse Racing Here is a famous story in Chinese history. That was about 2300 years ago ...

  6. hdoj 1052 Tian Ji -- The Horse Racing【田忌赛马】 【贪心】

    思路:先按从小到大排序, 然后从最快的開始比(如果i, j 是最慢的一端, flag1, flag2是最快的一端 ),田的最快的大于king的 则比較,如果等于然后推断,有三种情况: 一:大于则比較, ...

  7. hdu1052 Tian Ji -- The Horse Racing 馋

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:pid=1052">http://acm.hdu.edu.cn/showproblem.php ...

  8. Racing Car Computer dp

    Racing Car Computer Input: Standard Input Output: Standard Output   The racing cars of today are equ ...

  9. hdu-1052-Tian Ji -- The Horse Racing(经典)

    /* hdu-1052 Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/3 ...

随机推荐

  1. linux下rar包的解压方法

    linux下rar包的解压方法 学习了:https://blog.csdn.net/yonggeit/article/details/72190246?utm_source=itdadao&u ...

  2. caffe卷积层代码阅读笔记

    卷积的实现思想: 通过im2col将image转为一个matrix,将卷积操作转为矩阵乘法运算 通过调用GEMM完毕运算操作 以下两个图是我在知乎中发现的,"盗"用一下,确实非常好 ...

  3. Kubernetes - 配置Nginx-Ingress 作为服务发现

    添加 Kubernetes ConfigMap配置来自定义端口与服务的映射关系 配置文件, 有二个在默认空间下web服务和api服务分别映射到自定义端口 9001, 9002 apiVersion: ...

  4. json-server模拟接口获取mock数据

    转载:http://blog.csdn.net/stevennest/article/details/76167343 安装json-server 运行以下命令 cnpm install json-s ...

  5. hibernate学习系列-----(2)hibernate核心接口和工作机制

    在上一篇文章hibernate学习系列-----(1)开发环境搭建中,大致总结了hibernate的开发环境的搭建步骤,今天,我们继续了解有关hibernate的知识,先说说这篇文章的主要内容吧: C ...

  6. Java虚拟机对锁优化所做的努力(读书笔记)

    锁偏向      是一种加锁操作的优化手段,他的核心思想是:如果一个线程获得了锁,那么就进入偏向模式,当这个线程再次请求锁时,无须在做任何同步操作,因此在几乎没有锁竞争的场合,偏向锁是比较好的优化效果 ...

  7. ionic准备之angular基础——dom操作相关(6)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. Nmon命令行:Linux系统性能的监测利器

    如果你眼下正在寻找一款非常易于使用的Linux性能监测工具,那么我强烈推荐安装和使用Nmon命令行实用工具. Nmon监测工具 Nmon是一款面向系统管理员的调优和基准测量工具,可以用来显示关于下列方 ...

  9. Odoo 8,9,10 制造领料、入库 实践

    Odoo12 已经支持在 同一个仓库内,使用 投入/产品 库位, 不必采用本文的方法     Odoo 设计在 仓库/库存 进行生产,也就是 在 仓库/库存 领料,产出, 例如     如果要实现一般 ...

  10. STL学习笔记(string)

    动机 C++标准程序库中的string class使我们可以将string当做一个一般型别.我们可以像对待基本型别那样地复制.赋值和比较string, 再也不必但系内存是否足够.占用的内存实际长度等问 ...