https://icpcarchive.ecs.baylor.edu/index.php?

option=com_onlinejudge&Itemid=8&page=show_problem&problem=4496

In an attempt to demonstrate the practicality of electric cars, ElecCarCo is sponsoring a cross-country

road rally. There are n charging stations for the rally where cars may check in and charge their batteries.

The rally may require multiple days of travel. Each car can travel four hours (240 minutes) between

charges. A car must be plugged into a charger for two minutes for each minute of travel time. Cars

start the rally at noon on the first day, fully charged. Cars are permitted remain at a station even after

they are fully charged.

It is only possible to drive directly between select pairs of stations. Variations in trafc conditions,

road conditions, availability of HOV lanes, etc., result in different travel times along each route de-

pending upon the time of day at which travel along that route begins. All roads are two-way, and the

prevailing conditions affect travel in both directions.

The winner is the first car to reach checkpoint n − 1, starting form checkpoint 0. Other than the

starting and ending conditions, cars may pass through the stations in any order, and need not visit all

stations to complete the course.

Write a program to determine the earliest time, expressed as the total number of minutes elapsed

since the start of the rally, at which a car could reach the final checkpoint.

Input

There will be several test cases in the input. Each test case starts with a line containing n (1 ≤ n ≤ 500),

the number of stations, and m (1 ≤ m ≤ 1, 000), the number of connecting road segments.

This is followed by m blocks, each block describing one road segment. A road segment block has

the following structure:

Each block begins with a single line containing two integers, a and b (0 ≤ a, b ≤ n − 1, a ̸= b).

These numbers are the two checkpoints connected by that segment. The connections are undirected: a

segment permitting travel from station a to station b will also allow travel from station b to station a.

This is followed by from one to twenty ‘travel lines’ describing travel times. Each of the travel lines

contains 3 numbers: Start, Stop, (0 ≤ Start < Stop ≤ 1, 439), and T ime (0 < T ime < 1, 000). Start

and Stop are the time of day (expressed in minutes since midnight) described by this line, and T ime

is the travel time, in minutes, required to traverse this road segment if travel begins at any time in the

range [Start..Stop], inclusive. The first travel line in a block will have a start time of 0 (midnight, or

00:00). The final travel line in a block will have a stop time of 1439 (i.e., 23:59, or 1 less than 24 hours

times 60 minutes). Adjacent travel lines in the input will be arranged in order, and the start time of

any line after the first is one higher than the stop time of the preceding line. The travel lines will cover

all times from 00:00 to 23:59.

Input will end with a line with two 0s. All test cases will describe a course that can be completed

by the cars.

Output

For each test case, output a single integer representing the smallest number of minutes needed to

complete the rally. Output no spaces, and do not separate answers with blank lines.

Sample Input

4 4

0 1

0 1439 100

0 2

0 1439 75

1 3

0 720 150

721 824 100

825 1000 75

1001 1439 150

2 3

0 1439 150

3 2

0 1

0 10 200

11 1439 300

1 2

0 10 200

11 1439 300

4 3

0 1

0 719 500

720 1439 240

1 2

0 964 500

965 1439 2

2 3

0 971 500

972 1439 3

0 0

Sample Output

180

2360

255

题意:

给一张无向图,中午12:00从0点出发(充满电。支持240分钟的路程),在每一个点都能充电,充2分钟的电能跑1分钟,每条路按分钟分为若干个时间段。每一个时间段内通过这条路的时间为ti。问到达n-1号点最少要多少时间。

分析:

BFS+优先队列。按时间优先出队。要注意将电量的存储*2(当然有其它方法)。由于假设充电时间为奇数。用int存会丢失0.5。其次是要跑两个周期(两天)。

判重的话有非常多方法,较普遍的是开两维,位置和剩余电量,我的方法是开一维松弛,将电量化为0(x.time-x.power)。要小心的是时间段并非按题目描写叙述中说的分为1-20个(This is followed by from one to twenty ‘travel lines’ describing
travel times.),而是有24个,该死的非法数据,WA了我两天。都成WA掘机了。交了70多发才A,Total submissions才210。整个OJ的智商都被我拉低了。

  1. /*
  2. *
  3. * Author : fcbruce
  4. *
  5. * Time : Sun 05 Oct 2014 06:44:35 PM CST
  6. *
  7. */
  8. #include <cstdio>
  9. #include <iostream>
  10. #include <sstream>
  11. #include <cstdlib>
  12. #include <algorithm>
  13. #include <ctime>
  14. #include <cctype>
  15. #include <cmath>
  16. #include <string>
  17. #include <cstring>
  18. #include <stack>
  19. #include <queue>
  20. #include <list>
  21. #include <vector>
  22. #include <map>
  23. #include <set>
  24. #define sqr(x) ((x)*(x))
  25. #define LL long long
  26. #define itn int
  27. #define INF 0x3f3f3f3f
  28. #define PI 3.1415926535897932384626
  29. #define eps 1e-10
  30.  
  31. #ifdef _WIN32
  32. #define lld "%I64d"
  33. #else
  34. #define lld "%lld"
  35. #endif
  36.  
  37. #define maxm 2333
  38. #define maxn 507
  39.  
  40. using namespace std;
  41.  
  42. struct _record
  43. {
  44. int start[24],stop[24],time[24];
  45. int cnt;
  46. }w[maxm];
  47.  
  48. int fir[maxn];
  49. int u[maxm],v[maxm],nex[maxm];
  50. int e_max=0;
  51.  
  52. int vis[maxn];
  53. bool go[24];
  54.  
  55. inline int ReadInt()
  56. {
  57. int flag=0;
  58. int data=0;
  59. char ch=getchar();
  60. while (ch<'0' || ch>'9')
  61. {
  62. if (ch=='-') flag=1;
  63. ch=getchar();
  64. }
  65. do
  66. {
  67. data=data*10+ch-'0';
  68. ch=getchar();
  69. }while (ch>='0' && ch<='9');
  70. if (flag) data=-data;
  71. return data;
  72. }
  73.  
  74. inline void add_edge(int _u,int _v)
  75. {
  76. int &e=e_max;
  77. e++;
  78. u[e]=_u;v[e]=_v;
  79. nex[e]=fir[u[e]];fir[u[e]]=e;
  80. for (int i=0,start,stop=0,time,j=0;stop!=1439;i++)
  81. {
  82. start=ReadInt();
  83. stop=ReadInt();
  84. time=ReadInt();
  85. w[e].start[j]=w[e+1].start[j]=start;
  86. w[e].stop[j]=w[e+1].stop[j]=stop;
  87. w[e].time[j]=w[e+1].time[j]=time*2;
  88. w[e].cnt=w[e+1].cnt=++j;
  89. }
  90. e++;
  91. u[e]=_v;v[e]=_u;
  92. nex[e]=fir[u[e]];fir[u[e]]=e;
  93. }
  94.  
  95. struct Heap_node
  96. {
  97. int pos,time,power;
  98. bool operator < (const Heap_node &_)const
  99. {
  100. return time>_.time;
  101. }
  102. };
  103.  
  104. priority_queue<Heap_node> q;
  105.  
  106. int bfs(int s,int t,int start)
  107. {
  108. while (!q.empty()) q.pop();
  109. memset(vis,0x3f,sizeof vis);
  110. Heap_node iter=(Heap_node){s,start,480};
  111. q.push(iter);
  112.  
  113. while (!q.empty())
  114. {
  115. Heap_node x=q.top();q.pop();
  116. if (x.pos==t) return x.time-start;
  117. vis[x.pos]=min(vis[x.pos],x.time-x.power);
  118.  
  119. for (int e=fir[x.pos];~e;e=nex[e])
  120. {
  121. if (vis[v[e]]<x.time-x.power) continue;
  122. memset(go,0,sizeof go);
  123. int begin;
  124. for (int i=0;i<w[e].cnt;i++)
  125. if (x.time% 1440>=w[e].start[i] && x.time% 1440<=w[e].stop[i])
  126. {
  127. begin=i;
  128. break;
  129. }
  130.  
  131. if (w[e].time[begin]>480) goto too_far;
  132. if (x.power>=w[e].time[begin])
  133. {
  134. go[begin]=true;
  135. iter=(Heap_node){v[e],x.time+w[e].time[begin]/2,x.power-w[e].time[begin]};
  136. q.push(iter);
  137. }
  138. else
  139. {
  140. int charge=w[e].time[begin]-x.power;
  141. if (x.time % 1440+charge<=w[e].stop[begin])
  142. {
  143. go[begin]=true;
  144. iter=(Heap_node){v[e],x.time+charge+w[e].time[begin]/2,0};
  145. q.push(iter);
  146. }
  147. }
  148. too_far:
  149.  
  150. int plus=0;
  151. for (int j=begin+1;j<w[e].cnt*2+begin+1;j++)
  152. {
  153. int i=j%w[e].cnt;
  154. if (w[e].start[i]==0) plus+=1440;
  155. if (w[e].time[i]>480) continue;
  156. if (go[i]) continue;
  157. int power=x.power;
  158. int time=w[e].start[i]+plus-x.time% 1440;
  159. power+=time;
  160. power=min(480,power);
  161. if (power>=w[e].time[i])
  162. {
  163. go[i]=true;
  164. iter=(Heap_node){v[e],x.time+time+w[e].time[i]/2,power-w[e].time[i]};
  165. q.push(iter);
  166. }
  167. else
  168. {
  169. int charge=w[e].time[i]-power;
  170. if (w[e].start[i]+charge<=w[e].stop[i])
  171. {
  172. go[i]=true;
  173. iter=(Heap_node){v[e],x.time+time+charge+w[e].time[i]/2,0};
  174. q.push(iter);
  175. }
  176. }
  177. }
  178. }
  179. }
  180. }
  181.  
  182. int main()
  183. {
  184. #ifdef FCBRUCE
  185. freopen("/home/fcbruce/code/t","r",stdin);
  186. #endif // FCBRUCE
  187.  
  188. int n,m;
  189.  
  190. while (scanf("%d%d",&n,&m),n||m)
  191. {
  192. memset(fir,-1,sizeof fir);
  193. e_max=0;
  194.  
  195. for (int i=0,u,v;i<m;i++)
  196. {
  197. // scanf("%d%d",&u,&v);
  198. u=ReadInt();v=ReadInt();
  199. add_edge(u,v);
  200. }
  201.  
  202. printf("%d\n",bfs(0,n-1,720));
  203. }
  204.  
  205. return 0;
  206. }

版权声明:本文博客原创文章,博客,未经同意,不得转载。

UVALive 6485 Electric Car Rally (BFS,PQ)的更多相关文章

  1. UVALive 6665 Dragon’s Cruller --BFS,类八数码问题

    题意大概就是八数码问题,只不过把空格的移动方式改变了:空格能够向前或向后移动一格或三格(循环的). 分析:其实跟八数码问题差不多,用康托展开记录状态,bfs即可. 代码: #include <i ...

  2. UVALive 7297 Hounded by Indecision BFS

    题目链接:Hounded by Indecision 题意:map中给出小偷的位置,警察的位置.警察有一只狗,开始的时候警察和狗一起行动,也就是看做一个格子,当警察遇见小偷走过的格子时,狗就会嗅到它的 ...

  3. UVALive 3956 Key Task (bfs+状态压缩)

    Key Task 题目链接: http://acm.hust.edu.cn/vjudge/contest/129733#problem/D Description The Czech Technica ...

  4. UVALive 2520 Holedox Moving(BFS+状态压缩)

    这个题目在比赛的时候我们是没有做出来的,但是听到他们说进制哈希的时候,感觉真的是挺高端的,于是赛后开始补题,本着我的习惯在看题解之前自己再试着写一遍,我当时存储状态的方法是string + map,我 ...

  5. UVALive 2035 The Monocycle(BFS状态处理+优先队列)

    这道题目真是非常坎坷啊,WA了很多次,但所有的思路都是奔着广搜去想的,一开始出现了比答案大的数据,才想到了应该是优先队列,再说加上也肯定不会错.一开始我读错了题意,以为旋转并且前行需要的时间跟其他一样 ...

  6. UVALive 6908 Electric Bike dp

    Electric Bike 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8 ...

  7. UVALive-6485-Electric Car Rally(BFS)

    题目:点击打开链接 思路:对于当前位置的每个时间段都要走一遍(除了那些须要的时间比最大同意的时间还大的),用 整形 vis[当前位置][剩余油量] 标记. #include <cstdio> ...

  8. 最少步数(bfs)

    最少步数 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 这有一个迷宫,有0~8行和0~8列: 1,1,1,1,1,1,1,1,1 1,0,0,1,0,0,1,0,1 ...

  9. What a Ridiculous Election UVALive - 7672 (BFS)

    题目链接: E - What a Ridiculous Election  UVALive - 7672 题目大意: 12345 可以经过若干次操作转换为其它五位数. 操作分三种,分别为: 操作1:交 ...

随机推荐

  1. Nginx+ 多个Memcached+ 多个Tomcat集群配置来实现 sticky Session

    假如有 大于2 台的Tomcat servers,如何实现sticky session特点的高可靠web 服务? 方案设计: 前端使用nginx(最好是淘宝的 tengine)作为we 流量分发器,向 ...

  2. Python 奇葩语法

    a = 1, 2, 3 赋值后的结果,a == (1, 2, 3),将一个元组(tuple)赋给了变量 a (1, 2) + (3, ) ⇒ (1, 2, 3),并不能说明 tuple 可以添加新的元 ...

  3. php修改SESSION的有效生存时间

    如何修改SESSION的生存时间 我们来手动设置 Session 的生存期: <?phpsession_start(); // 保存一天 $lifeTime = 24 * 3600; setco ...

  4. 终端复用工具tmux的使用

    tmux的作用在于终端复用. 1. 在server上启动一个bash.并在里面执行tmux 2. 通过ssh远程登录server,执行tmux attach,就会切换到server上的那个bash中, ...

  5. Kolya and Tandem Repeat

     Kolya and Tandem Repeat time limit per test 2 seconds memory limit per test 256 megabytes input s ...

  6. python 设计模式之 单例模式

    单例模式是做为"全局变量"的替代品出现的.所以它具有全局变量的特点:全局可见.贯穿应用程序的整个生命期,保证在程序执行中,某个类仅仅存在一个实例,所以通常不希望类中的构造函数被调用 ...

  7. phpstorm常用快捷键有哪些(图解归类)

    phpstorm常用快捷键有哪些(图解归类) 一.总结 一句话总结: 10.方法参数提示,显示默认参数   解答:--------CTRL+P 13.显示类层级关系图,继承/实现关系   解答:--- ...

  8. [Redux] Important things in Redux

    Root Smart component can be overloaded, divide 'smart' component wisely & using Provider. Proble ...

  9. 【t054】糟糕的网络

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] 前几天sqybi 还在高高兴兴的用BOINC 完成着一个又一个的任务呢,但现在sqybi 突然变得闷闷 ...

  10. (ubuntu 下)tensorflow 的安装及版本升级

    对于 CPU 版本 pip3 install –upgrade tensorflow 对于 GPU 版本: pip3 install –upgrade tensorflow-gpu [TensorFl ...