uva 10801 Lift Hopping(dijkstra)
2015-06-12 19:49
507 查看
uva 10801 Lift Hopping
Ted the bellhop: "I'm coming up and if there isn't a dead body by the time I get there, I'll make one myself. You!" |
A skyscraper has no more than 100 floors, numbered from 0 to 99. It has n (1<=n<=5) elevators which travel up and down at (possibly) different speeds. For each i in {1, 2,... n}, elevator number i takes Ti (1<=Ti<=100)
seconds to travel between any two adjacent floors (going up or down). Elevators do not necessarily stop at every floor. What's worse, not every floor is necessarily accessible by an elevator.
You are on floor 0 and would like to get to floor k as quickly as possible. Assume that you do not need to wait to board the first elevator you step into and (for simplicity) the operation of switching an elevator on some floor always takes
exactly a minute. Of course, both elevators have to stop at that floor. You are forbiden from using the staircase. No one else is in the elevator with you, so you don't have to stop if you don't want to. Calculate the minimum number of seconds required to
get from floor 0 to floor k (passing floor k while inside an elevator that does not stop there does not count as "getting to floork").
Input
The input will consist of a number of test cases. Each test case will begin with two numbers, n and k, on a line. The next line will contain the numbers T1, T2,... Tn.
Finally, the next n lines will contain sorted lists of integers - the first line will list the floors visited by elevator number 1, the next one will list the floors visited by elevator number 2, etc.
Output
For each test case, output one number on a line by itself - the minimum number of seconds required to get to floor k from floor 0. If it is impossible to do, print "IMPOSSIBLE" instead.
Sample Input | Sample Output |
2 30 10 5 0 1 3 5 7 9 11 13 15 20 99 4 13 15 19 20 25 30 2 30 10 1 0 5 10 12 14 20 25 30 2 4 6 8 10 12 14 22 25 28 29 3 50 10 50 100 0 10 30 40 0 20 30 0 20 50 1 1 2 0 2 4 6 8 10 | 275 285 3920 IMPOSSIBLE |
In the first example, take elevator 1 to floor 13 (130 seconds), wait 60 seconds to switch to elevator 2 and ride it to floor 30 (85 seconds) for a total of 275 seconds.
In the second example, take elevator 1 to floor 10, switch to elevator 2 and ride it until floor 25. There, switch back to elevator 1 and get off at the 30'th floor. The total time is
10*10 + 60 + 15*1 + 60 + 5*10 = 285 seconds.
In example 3, take elevator 1 to floor 30, then elevator 2 to floor 20 and then elevator 3 to floor 50.
In the last example, the one elevator does not stop at floor 1.
题目大意:有一栋100层的大楼(标号为0~99),里面有n个电梯(不超过5个),以及要到达的层数(aid),然后是每个电梯走一层所需的时间,再n行就是对应每个电梯可以到达的层数,数量不定。然后每装换一次电梯需要等待60秒,问,最快能多快到达目标层数。
解题思路:建立邻接矩阵后,用dijkstra求最小消耗时间。注意特判目标层数为0的时候。
#include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <cstdlib> using namespace std; typedef long long ll; const int INF = 0x3f3f3f3f; int n, m, Cos[10], rec[105]; int gra[105][105], d[105], vis[105]; void dijkstra() { d[0] = 0; for (int i = 0; i < 99; i++) { int x, m = INF; for (int y = 0; y < 100; y++) { if (!vis[y] && d[y] <= m) { m = d[y]; x = y; } } vis[x] = 1; for (int y = 0; y < 100; y++) { if (d[y] > d[x] + gra[x][y] + 60) { d[y] = d[x] + gra[x][y] + 60; } } } } int main() { while (scanf("%d %d", &n, &m) == 2) { memset(gra, 0x3f, sizeof(gra)); memset(d, 0x3f, sizeof(d)); memset(vis, 0, sizeof(vis)); for (int i = 0; i < n; i++) { scanf("%d", &Cos[i]); } for (int k = 0; k < n; k++) { char ch = ' '; for (int i = 0; ch != '\n'; i++) { scanf("%d%c", &rec[i], &ch); for (int j = 0; j < i; j++) { int dis = abs(rec[i] - rec[j]) * Cos[k]; if (gra[rec[i]][rec[j]] > dis) gra[rec[i]][rec[j]] = gra[rec[j]][rec[i]] = dis; } } } if (m == 0) { printf("0\n"); continue; } dijkstra(); if (d[m] == INF) printf("IMPOSSIBLE\n"); else printf("%d\n", d[m] - 60); } return 0; }
相关文章推荐
- QQ登录集成到自己网站php代码(转载)
- 将QQ登录接口整合到你的网站和如何修改配置
- 4.1 MapReduce架构(1.0)
- linux 命令之 ping
- shell编程中 read export echo 理解
- django缓存架构
- apache2.4 以上的版本,cgi-bin中的文件不能运行。
- 使用方法拦截器MethodInterceptor和AOP统一处理log
- java web开发----tomcat安装与配置
- centos下快速安装JDK
- ubuntu 下mahout 的安装配置
- 高扩展性网站的50条原则
- ubuntu14.04.1 下hadoop2.6.0 的安装
- apache与nginx
- Linux与Windows文件共享
- PowerShell配置Exchange Server集线器配置
- keepalived+lvs/nginx 实现调度器高可用
- 删除grub引导的linux双系统
- [BigData]关于Hadoop学习笔记第三天(PPT总结)(一)
- tar文件的安装 基础