H - Lift Hopping UVA - 10801

▼魔方 西西 提交于 2020-02-22 06:42:55

1.题目
H - Lift Hopping UVA - 10801
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

2.题目大意
第一行给你n个电梯,你要到达第k层,接下来的一行告诉你1~n号电梯上一个楼层需要的时间,然后接下来n行告诉你1——n号电梯能去哪些楼层。然后你换一个电梯需要等待60s,问你从0层到达k层的最少时间,不能到达那个楼层就IMPOSSIBLE.

3.解题思路
在建图的时候就把某层到某层的所需时间弄出来,然后用floyd时记得换了电梯要加多60s。然后要注意输入每个电梯能去的楼层的时候,数量是不确定的,所以要用字符串。

4.floyd
graph[i][j] = min(graph[i][j], graph[i][k] + graph[k][j] + 60);
//要么从单一电梯直接从i到j,或者先i->k然后再k->j,要加上换电梯的时间60s

5.AC代码

#include <iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
#define maxn 120
#define M 120
const int INF =0x3f3f3f3f;
int n,m;
int T[maxn];
int graph[M][M];

void flyod()
{
    for(int k = 0; k<=100; k++) //中间点
    {
        for(int i = 0; i<=100; i++) //出发点
        {
            if(i!=k)
            {
                for(int j = 0; j<=100; j++)//终点
                {
                    if(i!=j)
                        graph[i][j] = min(graph[i][j], graph[i][k] + graph[k][j] + 60);//要么从单一电梯直接从i到j,或者先i->k然后再k->j,要加上换电梯的时间60s
                }
            }
        }
    }
}
int main()
{
    while(scanf("%d%d",&n,&m) != EOF)
    {
        for(int i = 0; i < n; i++)
        {
            scanf("%d",&T[i]);
        }
        //初始化图
        for(int i = 0; i <= 100; i++)
            for(int j = 0; j <= 100; j++)
            {
                if(i == j)
                    graph[i][j] = 0;
                else
                    graph[i][j] = INF;
            }

        getchar();
        for(int i = 0; i < n; i++)
        {
            //处理进来的i号电梯能去的层

            int a[225];
            int num = 0;
            memset(a,0,sizeof(a));
            char c;
            int d;
            int k = 0;
            while(scanf("%d%c",&d,&c))
            {
                a[k++] = d;
                if(c=='\n')
                    break;
            }
            //建立图,将每层楼去另一层楼的花费时间建立
            for(int s = 1; s<k; s++)
            {
                for(int j = 0; j<s; j++)
                {
                    graph[a[j]][a[s]] = min( graph[a[j]][a[s]], T[i]*abs(a[s] - a[j]) );
                    graph[a[s]][a[j]] = min( graph[a[s]][a[j]], T[i]*abs(a[s] - a[j]) );
                }
            }
        }
        if(m==0)
        {
            printf("0\n");
            continue;
        }
        flyod();

        if(graph[0][m] == INF)
            printf("IMPOSSIBLE\n");
        else
            printf("%d\n",graph[0][m]);
    }
    return 0;
}

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!