UVA 10801 - Lift Hopping(dijkstra)

来源:互联网 发布:罗技鼠标知乎 编辑:程序博客网 时间:2024/05/23 19:16

Problem ?
Lift Hopping
Time Limit: 1 second

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!"
Robert Rodriguez, "Four Rooms."

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 floor k").

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 T1T2,... 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 InputSample Output
2 3010 50 1 3 5 7 9 11 13 15 20 994 13 15 19 20 25 302 3010 10 5 10 12 14 20 25 302 4 6 8 10 12 14 22 25 28 293 5010 50 1000 10 30 400 20 300 20 501 120 2 4 6 8 10
2752853920IMPOSSIBLE

Explanation of examples

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.

题意:n个电梯,目标aid层,每个电梯的时间ti,每个电梯能到的层数,转电梯需要60秒,求最少时间,如果到不了就输出IMPOSSIBLE.

思路:dijkstra最短路,因为只有换电梯的时候才可能进行松弛操作,所以只需要在那加60秒即可。

代码:

#include <stdio.h>#include <string.h>#define INF 0x3f3f3f3f#define min(a, b) (a)<(b)?(a):(b)int n, aid, time[6], ele[6][105], map[6][105][105];void init() {    memset(ele, -1, sizeof(ele));    memset(map, INF, sizeof(map));    for (int i = 1; i <= n; i ++)scanf("%d", &time[i]);    for (int i = 1; i <= n; i ++) {int a; char c; int an = 0;while (scanf("%d%c", &a, &c)) {    ele[i][an ++] = a;    if (c == '\n')break;}    }    for (int k = 1; k <= n; k ++) {for (int i = 0; ele[k][i] != -1; i ++) {    for (int j = i + 1; ele[k][j] != -1; j ++) {int a = ele[k][i], b = ele[k][j];int c = a - b > 0 ? a - b : b - a;map[k][a][b] = map[k][b][a] = c * time[k];    }}    }    for (int i = 1; i <= n; i ++)for (int j = 0; j < 100; j ++)    map[i][j][j] = 0;}int dij(int s, int ki) {    int vis[105], d[105], index;     memset(vis, 0, sizeof(vis));    memset(d, INF, sizeof(d));    vis[s] = 1;    for (int i = 0; i < 100; i ++)d[i] = map[ki][s][i];    for (int i = 1; i < 100; i ++) {int Min = INF;for (int j = 0; j < 100; j ++) {    if (Min > d[j] && !vis[j]) {Min = d[j];index = j;    }}if (Min == INF)    return d[aid];vis[index] = 1;for (int j = 0; j < 100; j ++) {    if (!vis[j]) {for (int k = 1; k <= n; k ++)    d[j] = min(d[j], d[index] + map[k][index][j] + 60);    }}    }    return d[aid];}void solve() {    int ans = INF;    init();    for (int i = 1; i <= n; i ++)ans = min(ans, dij(0, i));    if (ans == INF)printf("IMPOSSIBLE\n");    elseprintf("%d\n", ans);}int main() {    while (~scanf("%d%d", &n, &aid)) {solve();    }    return 0;}