Lift Hopping

来源:互联网 发布:mysql系统表 编辑:程序博客网 时间:2024/05/18 22:54

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 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
一座大楼0到99层,最多有5个电梯,每个电梯有自己的速度,和自己独特的停靠楼层,换电梯需要额外的60s,问到达目标楼层的最短时间。
图实在是太明显了,直接100层而已,然后直接用迪杰斯特拉一个一个找。
一开始赋值表达式写了比较之前的电梯与之后电梯是否一致……后来发现根本不需要比较,因为如果之前就是这部电梯,那么到达的那个点一定在之前搜索过了,而且是不加60s的情况,所以就算表达式中加60s与之前比较,也会被过滤掉。

#include<cstdio>#include<cstring>#include<iostream>#include<queue>#include<vector>#include<algorithm>#include<string>#include<cmath>#include<set>using namespace std;typedef long long ll;const int inf = 0x3f3f3f3f;struct Node{    int v;    int leap[105];}node[10];int vis[105], d[105];int main(){    int i, j, m, n, ans, t, k;    while (scanf("%d%d", &n, &k) != EOF)    {        for (i = 1; i <= n; i++)        {            cin >> node[i].v;            for (j = 0; j <= 100; j++)                node[i].leap[j] = 0;        }        int cnt = 1;        while (cnt <= n)        {            int x;            char c;            c = 'a';            while (c != '\n'){                cin >> x;                node[cnt].leap[x] = 1;                c = getchar();            }            cnt++;        }        memset(vis, 0, sizeof(vis));        for (i = 1; i <= 100; i++)            d[i] = inf;        d[0] = 0;        vis[0] = 1;        for (i = 1; i <= n; i++)        {            if (node[i].leap[0])            {                for (j = 1; j <= 100; j++)                if (node[i].leap[j])                {                    d[j] = min(d[j], node[i].v*j);                }            }        }        for (i = 1; i <= 100; i++)        {            int tempx, tempm;            tempx = tempm = inf;            for (j = 1; j <= 100; j++)            {                if (!vis[j] && d[j] <= tempm)                {                    tempm = d[j];                    tempx = j;                }            }            if (tempx == inf)break;            vis[tempx] = 1;            for (j = 1; j <= 100; j++)            {                int k;                for (k = 1; k <= n; k++)                {                    if (node[k].leap[j] && node[k].leap[tempx])                    {                        d[j] = min(d[j], d[tempx] + node[k].v*abs(tempx - j) + 60);                    }                }            }        }        if (d[k] == inf)            cout << "IMPOSSIBLE" << endl;        else            cout << d[k] << endl;    }    return 0;}
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 无创检查高风险怎么办 唐氏筛查21三体高危怎么办 唐筛年龄高风险怎么办 21三体综合症高风险怎么办 朋友深陷李强365怎么办 飞机上烟瘾犯了怎么办 怀孕一个月吸烟了怎么办 烟瘾犯了没烟怎么办 押金交了不租了怎么办 买车首付款不够怎么办 双11订金不退怎么办 在商场买到假货怎么办 网上买到假手机怎么办 网上买了假手机怎么办 网银转账被骗了怎么办 支付宝被骗了钱怎么办 被支付宝骗了钱怎么办 头发出油怎么办小妙招 照相的时候脸歪怎么办 怀孕两个月同床了怎么办 小姐被警察抓了怎么办 我的世界迷路了怎么办 砸到手指甲很痛怎么办 手被锤子砸肿了怎么办 拇指突然疼肿了怎么办 手指甲压紫了痛怎么办 砸到手指头肿了怎么办 小婴儿体重不长怎么办 2岁半宝宝不说话怎么办 米兰叶子掉光了怎么办 qq雷霆战机闪退怎么办 微信限额20万怎么办 欧拉方程x<0怎么办 柿子核吃下去了怎么办 橡胶底白色变黄怎么办 20了丁丁还很小怎么办 5角硬币吞进肚子怎么办 孩子吞了5角硬币怎么办 左腿比右腿微粗怎么办 一个腿长一个腿短怎么办 刚买的毛笔很硬怎么办