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

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <queue>
  4. using namespace std;
  5.  
  6. #define MAXN 500005
  7. #define inf 0xfffffff
  8.  
  9. struct Bian
  10. {
  11. int e;
  12. int a,b,t;
  13. int next;
  14. }bian[MAXN];
  15.  
  16. int headlist[];
  17. int d[];
  18. int vis[];//其实我真不知道这个有什么用,不加也能过,有时候还能更快,但是每次看到spfa算法都加上我就加上了
  19. int Case=;
  20.  
  21. int check(int time,Bian x)//通过这条边需要的时间
  22. {
  23. int k=time%(x.a+x.b);
  24. if (k+x.t<=x.a)
  25. {
  26. return x.t;
  27. }
  28. return x.a+x.b-k+x.t;
  29. }
  30.  
  31. void spfa(int n,int m,int star,int end)
  32. {
  33. queue<int> Q;
  34. int i,x,v,y;
  35. for (i=;i<=n;i++)
  36. {
  37. d[i]=inf;
  38. vis[i]=;
  39. }
  40. d[star]=;
  41. Q.push(star);
  42. while (!Q.empty())
  43. {
  44. x=Q.front();
  45. Q.pop();
  46. vis[x]=;
  47. for (i=headlist[x];i!=-;i=bian[i].next)
  48. {
  49. v=bian[i].e;
  50. y=check(d[x],bian[i]);
  51. if (d[v]>d[x]+y)
  52. {
  53. d[v]=d[x]+y;
  54. if (!vis[v])
  55. {
  56. vis[v]=;
  57. Q.push(v);
  58. }
  59. }
  60. }
  61. }
  62. printf("Case %d: %d\n",++Case,d[end]);
  63. }
  64.  
  65. int main()
  66. {
  67. int n,m,s,e;
  68. int u,v,a,b,t;
  69. int i;
  70. while (scanf("%d%d%d%d",&n,&m,&s,&e)!=EOF)
  71. {
  72. for (i=;i<=n;i++)
  73. headlist[i]=-;
  74.  
  75. for (i=;i<=m;i++)
  76. {
  77. scanf("%d%d%d%d%d",&u,&v,&a,&b,&t);
  78. if (t>a) continue;
  79. bian[i].e=v;
  80. bian[i].a=a;
  81. bian[i].b=b;
  82. bian[i].t=t;
  83. bian[i].next=headlist[u];//这叫邻接表吧
  84. headlist[u]=i;
  85. }
  86. spfa(n,m,s,e);
  87. }
  88. return ;
  89. }

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. DELLR720 独立显卡DVI转VGA问题

    第一种情况:DELLR720默认不接独立显卡,直接用板载VGA输出,不需要调system blos 第二种情况:DELLR720接独立显卡,需要DVI输出,需要更改系统blos system blos ...

  2. scrapy爬虫程序xpath中文编码报错

    2017-03-23 问题描述: #选择出节点中“时间”二字 <h2>时间</h2> item["file_urls"]= response.xpath(& ...

  3. Shell命令-----VI

    vi的基本操作 a) 进入vi     在系统提示符号输入vi及文件名称后,就进入vi全屏幕编辑画面: $ vi file  不过有一点要特别注意,就是您进入vi之后,是处于「命令行模式(comman ...

  4. 模拟服务器MockServer之Moco详细介绍

    转载:http://blog.csdn.net/vite_s/article/details/54583243 前面一篇介绍了如何用mockito来测试我们的一些异步任务,例如网络请求时候的异步回调. ...

  5. 通过Fsharp探索Enterprise Library Exception

    Exception怎么生成是一回事,怎么展示又是还有一回事了. Exception Block主要关注的点在于Exception信息的展示.Exception不同于一般的log信息,是系统设计者未考虑 ...

  6. 点击Div,显示其innerHTML

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. react-native 中使用 mobx

    1. 介绍 1.1. 原理 React的render是 状态 转化为树状结构的渲染组件的方法而MobX提供了一种存储,更新 状态 的方法React 和 MobX都在优化着软件开发中相同的问题.Reac ...

  8. JavaScript创建块级作用域

    1.JavaScript创建块级作用域 (1)方法一:ES6 (2)方法二:闭包 2.示例 <!DOCTYPE html> <html lang="zh"> ...

  9. 最简单的基于FFmpeg的移动端样例附件:Android 自带播放器

    ===================================================== 最简单的基于FFmpeg的移动端样例系列文章列表: 最简单的基于FFmpeg的移动端样例:A ...

  10. 【Lucene】Apache Lucene全文检索引擎架构之入门实战1

    Lucene是一套用于全文检索和搜寻的开源程式库,由Apache软件基金会支持和提供.Lucene提供了一个简单却强大的应用程式接口,能够做全文索引和搜寻.在Java开发环境里Lucene是一个成熟的 ...