很简单的一道最短路问题。分情况处理赛道的打开和关闭。

Code

  1. /**
  2. * UVa
  3. * Problem#12661
  4. * Accepted
  5. * Time:50ms
  6. */
  7. #include<iostream>
  8. #include<fstream>
  9. #include<sstream>
  10. #include<cstdio>
  11. #include<cstdlib>
  12. #include<cstring>
  13. #include<ctime>
  14. #include<cctype>
  15. #include<cmath>
  16. #include<algorithm>
  17. #include<stack>
  18. #include<queue>
  19. #include<set>
  20. #include<map>
  21. #include<vector>
  22. using namespace std;
  23. typedef bool boolean;
  24. #define smin(a, b) (a) = min((a), (b))
  25. #define smax(as, b) (a) = max((a), (b))
  26. template<typename T>
  27. inline boolean readInteger(T& u){
  28. char x;
  29. int aFlag = ;
  30. while(!isdigit((x = getchar())) && x != '-' && x != -);
  31. if(x == -) return false;
  32. if(x == '-'){
  33. x = getchar();
  34. aFlag = -;
  35. }
  36. for(u = x - ''; isdigit((x = getchar())); u = (u << ) + (u << ) + x - '');
  37. ungetc(x, stdin);
  38. u *= aFlag;
  39. return true;
  40. }
  41.  
  42. typedef class Edge{
  43. public:
  44. int end;
  45. int next;
  46. int open;
  47. int close;
  48. int t;
  49. Edge(const int end = , const int next = , const int open = , const int close = , const int t = ):end(end), next(next), open(open), close(close), t(t){ }
  50. int across(int arrived){
  51. int re = arrived % (open + close);
  52. if(re >= && re + t <= open) return arrived + t;
  53. return (arrived / (open + close) + ) * (open + close) + t;
  54. }
  55. }Edge;
  56.  
  57. typedef class MapManager{
  58. public:
  59. int ce;
  60. Edge* edges;
  61. int* h;
  62. MapManager():ce(), edges(NULL), h(NULL){ }
  63. MapManager(int points, int limit):ce(){
  64. h = new int[(const int)(points + )];
  65. edges = new Edge[(const int)(limit + )];
  66. memset(h, , sizeof(int) * (points + ));
  67. }
  68. inline void addEdge(int from, int end, int open, int close, int t){
  69. if(t > open) return; //无法通过
  70. edges[++ce] = Edge(end, h[from], open, close, t);
  71. h[from] = ce;
  72. }
  73. Edge& operator [](int pos){
  74. return edges[pos];
  75. }
  76. void clear(){
  77. delete[] edges;
  78. delete[] h;
  79. ce = ;
  80. }
  81. }MapManager;
  82.  
  83. #define m_begin(g, i) (g).h[(i)]
  84.  
  85. int n, m, from, _end;
  86. MapManager g;
  87.  
  88. inline boolean init(){
  89. if(!readInteger(n)) return false;
  90. readInteger(m);
  91. readInteger(from);
  92. readInteger(_end);
  93. g = MapManager(n, m);
  94. for(int i = , u, v, a, b, t; i <= m; i++){
  95. readInteger(u);
  96. readInteger(v);
  97. readInteger(a);
  98. readInteger(b);
  99. readInteger(t);
  100. g.addEdge(u, v, a, b, t);
  101. }
  102. return true;
  103. }
  104.  
  105. boolean* visited;
  106. queue<int> que;
  107. int* f;
  108. inline int spfa(int s, int t){
  109. que.push(s);
  110. visited[s] = true;
  111. f[s] = ;
  112. while(!que.empty()){
  113. int e = que.front();
  114. que.pop();
  115. visited[e] = false;
  116. for(int i = m_begin(g, e); i != ; i = g[i].next){
  117. int& eu = g[i].end;
  118. int cmp = g[i].across(f[e]);
  119. if(cmp < f[eu]){
  120. f[eu] = cmp;
  121. if(!visited[eu] && eu != t){
  122. que.push(eu);
  123. visited[eu] = true;
  124. }
  125. }
  126. }
  127. }
  128. return f[t];
  129. }
  130.  
  131. inline void solve(){
  132. visited = new boolean[(const int)(n + )];
  133. f = new int[(const int)(n + )];
  134. memset(visited, false, sizeof(boolean) * (n + ));
  135. memset(f, 0x7f, sizeof(int) * (n + ));
  136. int res = spfa(from, _end);
  137. printf("%d\n", res);
  138. }
  139.  
  140. inline void clear(){
  141. g.clear();
  142. delete[] visited;
  143. delete[] f;
  144. }
  145.  
  146. int main(){
  147. int kase = ;
  148. while(init()){
  149. printf("Case %d: ", kase++);
  150. solve();
  151. clear();
  152. }
  153. return ;
  154. }

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

  1. UVa - 12661 - Funny Car Racing

    先上题目: 12661 Funny Car RacingThere is a funny car racing in a city with n junctions and m directed ro ...

  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 11090 Going in Cycle!! SPFA判断负环+二分

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

  9. UVA 11280 - Flying to Fredericton SPFA变形

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&c ...

随机推荐

  1. CCCC L2-022. 重排链表

    题解:直接list模拟,头尾两个指针,分别将头尾元素push到另一个list里面,输处输入方式同上一篇 坑:第一发卡了第二个样例,第二发卡了第4个,莫名其妙,所以把两个代码合起来,然后强行ac了. # ...

  2. css3的一个小demo(箭头hover变化)

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

  3. Mybatis插入数据后返回主键id

    有时候使用mybatis插入数据后,需要用到记录在数据库中的自增id,可以利用keyProperty来返回,赋值给实体类中的指定字段. 单条记录插入并返回 First, if your databas ...

  4. linux环境下python的部署

    linux系统环境自带python2.6,但有时我们项目使用的版本可能是3.x以上等等,此时我们需要在linux中再安装项目所需的python版本,此时就涉及多版本共存问题了,很多同学在安装多个版本P ...

  5. CentOS网卡配置大全

    持续更新中... 1.配置机器名 2.配置网卡属性 3.问题解决 3.1解决网卡经常需要手动重连问题 vi /etc/sysconfig/network-scripts/ifcfg-ens33 把ON ...

  6. mysql 权限管理 针对库 授权 db.*

    需求 只放行user表 db1库的select权限 mysql> grant select on db1.* to 'mike'@'localhost'; Query OK, rows affe ...

  7. edgeR使用学习【转载】

    转自:http://yangl.net/2016/09/27/edger_usage/ 1.Quick start 2. 利用edgeR分析RNA-seq鉴别差异表达基因: #加载软件包 librar ...

  8. FPKM\RPKM\TPM学习[转载]

    转自:http://www.360doc.com/content/18/0112/02/50153987_721216719.shtml 1.问题提出 在RNA-Seq的分析中,对基因或转录本的rea ...

  9. MySQL架构简介

  10. [LeetCode] 190. Reverse Bits_Easy tag: Bit Manipulation

    Reverse bits of a given 32 bits unsigned integer. Example: Input: 43261596 Output: 964176192 Explana ...