UVA 10801 Lift Hopping(最短路)

来源:互联网 发布:小说 炫浪网络社区 编辑:程序博客网 时间:2024/06/03 17:58

思路:以每一层楼为顶点,每个电梯可以到达的两层楼之间的秒数为每一条边的权值,以此构建一个无向图。然后利用dijkstra求出最短的时间,注意每次换乘电梯需要等待60s(因为同一个电梯上的楼层是相互可达的,所以我们只有通过另外一个电梯找到了更小的搭乘时间时候我们才会执行松弛操作),因此每转一个定点需要加60s时间(注意初始定点不需要60s的等待)。


#include<bits/stdc++.h>using namespace std;#define INF 1e9int n,k;int w[105][105];int d[105];int vis[105];int T[105];int a[105];int main(){while (scanf("%d%d",&n,&k)!=EOF){for (int i = 0;i<n;i++){w[i][i]=INF;for (int j = i+1;j<n;j++)w[i][j]=w[j][i]=INF;}for (int i = 0;i<n;i++)scanf("%d",&T[i]);char ch;for (int i = 0;i<n;i++){int pos = 0;        do{scanf("%d",&a[pos++]);}while (getchar()!='\n');for (int j = 0;j<pos;j++)for (int k = j;k<pos;k++){int temp = abs(a[j]-a[k])*T[i];                    if(temp<w[a[j]][a[k]]){                         w[a[j]][a[k]] = w[a[k]][a[j]] = temp;}}}memset(vis,0,sizeof(vis));for (int i = 0;i<n;i++)    d[i]=INF;d[0]=0;queue<int>q;q.push(0);while (!q.empty())        {int u = q.front();q.pop();vis[u]=0;for (int i = 0;i<n;i++){                if (u==0){if (d[i]>d[u]+w[u][i]){d[i]=d[u]+w[u][i];if (!vis[i]){vis[i]=1;q.push(i);}}}else if (d[i]>d[u]+w[u][i]+60){d[i]=d[u]+w[u][i]+60;if (!vis[i]){vis[i]=1;q.push(i);}}}}        if (d[k]==INF)printf("IMPOSSIBLE\n");elseprintf("%d\n",d[k]);}}


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 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.
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.
Sample Input
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
Sample Output
275
285
3920
IMPOSSIBLE
0 0
原创粉丝点击