先上题目:

12661 Funny Car Racing
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 Output
Case 1: 20
Case 2: 9

  题意:给出一个有向图,每条边都有三个值,打开时间间隔,关闭时间间隔,通过它需要多少时间,路的开关是循环往复的。给你起点和终点,问你最少需要多少时间才可以从起点到达终点(保证一定存在这种路)。

  这其实是求最短路,用DIJ求最短路,然后对于从某个点开始可以到达的下一个点,在分析是否需要更新的时候,需要判断当前时刻路开了没有,如果开了看看够不够时间过去,然后根据这些情况更新最小值就可以了。这里需要注意的东西就是这事有向图不是无向图。

上代码:

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. #define MAX 50002
  5. #define ll long long
  6. #define INF (((ll)1)<<60)
  7. using namespace std;
  8.  
  9. typedef struct edge{
  10. int to,next;
  11. ll a,b,c,s;
  12. }edge;
  13.  
  14. edge e[MAX<<];
  15. int p[],tot;
  16. int n,m,st,ed;
  17. ll dis[MAX];
  18.  
  19. void dij(){
  20. bool f[]={};
  21. for(int i=;i<=n;i++){
  22. dis[i]=INF;
  23. }
  24. dis[st]=;
  25. for(int i=;i<n;i++){
  26. int u;
  27. ll dd=INF;
  28. for(int j=;j<=n;j++){
  29. if(!f[j] && dd>dis[j]) {
  30. dd=dis[j];
  31. u=j;
  32. }
  33. }
  34. if(dd==INF) break;
  35. f[u]=;
  36. for(int j=p[u];j!=-;j=e[j].next){
  37. ll t=dis[u];
  38. ll r=t%e[j].s;
  39. if(r>=e[j].a){
  40. t+=(e[j].s-r)+e[j].c;
  41. }else{
  42. if(e[j].a-r>=e[j].c) t+=e[j].c;
  43. else t+=e[j].s-r+e[j].c;
  44. }
  45. dis[e[j].to]=min(t,dis[e[j].to]);
  46. }
  47. }
  48. }
  49.  
  50. inline void add(int u,int v,int a,int b,int c){
  51. e[tot].to=v; e[tot].next=p[u];
  52. e[tot].a=a; e[tot].b=b; e[tot].c=c; e[tot].s=a+b;
  53. p[u]=tot++;
  54. }
  55.  
  56. int main()
  57. {
  58. int t,u,v,a,b,c;
  59. //freopen("data.txt","r",stdin);
  60. t=;
  61. while(scanf("%d %d %d %d",&n,&m,&st,&ed)!=EOF){
  62. memset(p,-,sizeof(p));
  63. tot=;
  64. for(int i=;i<m;i++){
  65. scanf("%d %d %d %d %d",&u,&v,&a,&b,&c);
  66. if(c<=a){
  67. add(u,v,a,b,c);
  68. //add(v,u,a,b,c);
  69. }
  70. }
  71. dij();
  72. printf("Case %d: %lld\n",t++,dis[ed]);
  73.  
  74. }
  75. return ;
  76. }

/*12661*/

UVa - 12661 - Funny Car Racing的更多相关文章

  1. UVa 12661 Funny Car Racing - spfa

    很简单的一道最短路问题.分情况处理赛道的打开和关闭. Code /** * UVa * Problem#12661 * Accepted * Time:50ms */ #include<iost ...

  2. UVa 12661 - Funny Car Racing(Dijkstra)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  3. UVA 12661 Funny Car Racing 有趣的赛车比赛(最短路,变形)

    题意:赛道有n个交叉点,和m条单向路径(有重边),每条路都是周期性关闭的,且通过仍需一段时间.在比赛开始时,所有道路刚好打开,选择进入该道路必须满足“在打开的时间段进入,在关闭之前出来”,即不可在路上 ...

  4. UVa 12661 Funny Car Racing (dijkstra)

    题意:给定一个有向图,每条路有5个整数修饰,u, v, a, b, t,表示起点为u,终点为v,打开时间a,关闭时间为b,通过时间为t,打开关闭是交替进行的, 问你从s到t最短时间是多少. 析:使用d ...

  5. UVA - 12661 Funny Car Racing (Dijkstra算法)

    题目: 思路: 把时间当做距离利用Dijkstra算法来做这个题. 前提:该结点e.c<=e.a,k = d[v]%(e.a+e.b); 当车在这个点的1处时,如果在第一个a这段时间内能够通过且 ...

  6. UVa 12661 Funny Car Racing【 dijkstra 】

    题意:给出n个点,m条路,每条路用5个整数表示u,v,a,b,t u表示这条路的起点,v表示终点,a表示打开时间,b表示关闭时间,t表示通过这条道路需要的时间 看的紫书,因为边权不再仅仅是路上的时间, ...

  7. UVa 12661 (单源最短路) Funny Car Racing

    题意: 有一个赛车跑道,可以看做一个加权有向图.每个跑道(有向边)还有一个特点就是,会周期性地打开a秒,然后关闭b秒.只有在赛车进入一直到出来,该跑道一直处于打开状态,赛车才能通过. 开始时所有跑道处 ...

  8. UVA 12661(动态权值+最短路,dij)

    题意:赛车背景,给你n个节点,m条边的图以及起点和终点:其中每条边的信息包括u(起点),v(终点),a(开启的时间),b(关闭的时间),d(通过这条道路的时间):求最短通过的时间,其中车在进的时候,保 ...

  9. 紫书 例题11-11 UVa 12661 (dihkstra变形)

    这道题主要比较权值的时候要改变一下,其他地方基本一样. 比较权值的时候要考虑边的时间与a, b 可以设相对于当前边的时间now, 则now = d[u] % (a+b), 也就是当前这个边进行到整个a ...

随机推荐

  1. bzoj2242 [SDOI2011]计算器——BSGS

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2242 第一次写BSGS,参考了好多好多博客: 然而看到的讲解和模板是一种写法,这道题的网上题 ...

  2. HIT1917Peaceful Commission(2-SAT)

    Peaceful Commission   Source : POI 2001   Time limit : 10 sec   Memory limit : 32 M Submitted : 2112 ...

  3. [Swift通天遁地]六、智能布局-(1)给视图添加尺寸和中心点的约束

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  4. SpringBoot集成Swagger2 以及汉化 快速教程

    (一) Swagger介绍 Swagger 是一款RESTFUL接口的文档在线自动生成+功能测试功能软件 (二)为什么使用Swagger 在现在的开发过程中还有很大一部分公司都是以口口相传的方式来进行 ...

  5. 51nod 1222 莫比乌斯反演

    思路: yhx找的反演题 题解已经烂大街了 #pragma GCC optimize("O3") //By SiriusRen #include <bits/stdc++.h ...

  6. day03_12/13/2016_bean的管理之依赖注入

  7. ORACLE SEQUENCE用法(转)

    ORACLE SEQUENCE用法 在oracle中sequence就是序号,每次取的时候它会自动增加.sequence与表没有关系. 1.Create Sequence     首先要有CREATE ...

  8. 更新换代之requests库

    好久不更新博客了... 之前的博文都是通过urllib2进行http访问,接下来我要说一个利器啊!requests模块,无法用语言对他进行赞扬了,需要的,有兴趣的,可以去了解下,移步官方中文文档: R ...

  9. C#入门经典 Chapter4 流程控制

    4.1布尔逻辑 布尔比较运算符 ==  !=   <   >    <=    >= 处理布尔值的布尔值运算符 ! & | ^(异或) 条件布尔运算符 &&am ...

  10. iOS UIWebView 访问https绕过证书验证的方法

    @implementation NSURLRequest (NSURLRequestWithIgnoreSSL) + (BOOL)allowsAnyHTTPSCertificateForHost:(N ...