• uva 10801(最短路)


    题目大意:

    有一层不超过100层的大楼, 有n个电梯,它们的速度都不同。 而且每个电梯只能到达指定的那些楼层,而且它们都有各自的速度(即上升一层或下降一层所用的时间)。 如果一个人在某层走出电梯,要换一个电梯乘,那么他要等60秒(不管要等的是那个电梯,即使是刚刚出来的那个电梯也要等60秒)。在0层搭电梯出发时不需要等待。一个人从0层开始,目的地是k层, 现在要搭这些电梯,问最少需多少时间。

    #include <cstdio>
    #include <iostream>
    #include <sstream>
    #include <cmath>
    #include <cstring>
    #include <cstdlib>
    #include <string>
    #include <vector>
    #include <map>
    #include <set>
    #include <queue>
    #include <stack>
    #include <algorithm>
    using namespace std;
    #define ll int
    #define _cle(m, a) memset(m, a, sizeof(m))
    #define repu(i, a, b) for(int i = a; i < b; i++)
    #define repd(i, a, b) for(int i = b; i >= a; i--)
    #define sfi(n) scanf("%d", &n)
    #define pfi(n) printf("%d
    ", n)
    #define sfi2(n, m) scanf("%d%d", &n, &m)
    #define sfd2(n, m) scanf("%lf%lf", &n, &m)
    #define pfi2(n, m) printf("%d %d
    ", n, m)
    #define pfi3(a, b, c) printf("%d %d %d
    ", a, b, c)
    #define maxn 100
    const int INF = 0x3f3f3f3f;
    int w[maxn][maxn];
    int c[maxn];
    bool vis[maxn];
    int d[maxn];
    int t[6];
    inline void SPFA(int src){
        memset(vis, 0, sizeof(vis));
        for(int i = 0; i < maxn; ++i) d[i] = INF;
        d[src] = 0;
        queue<int> q;
        q.push(src);
        while(!q.empty()){
            int u = q.front(); q.pop();
            vis[u] = false;
            for(int i = 0; i < maxn; ++i){
                if(u == 0){
                    if(d[i] > d[u] + w[u][i]){
                        d[i] = d[u] + w[u][i];
                        if(!vis[i]){
                            vis[i] = true;
                            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] = true;
                        q.push(i);
                    }
                }
            }
        }
        return ;
    }
    
    int main()
    {
        int n, k;
        while(~sfi2(n, k))
        {
            _cle(w, 0x3f);
            repu(i, 0, n) sfi(t[i]);
            int la = -1, cur;
            char cc;
            repu(i, 0, n)
            {
                int m = 0;
                while(1)
                {
                    sfi(c[m++]);
                    cc = getchar();
                    if(cc == '
    ')
                    {
                        repu(j, 0, m) repu(k, j + 1, m)
                        w[c[k]][c[j]] = w[c[j]][c[k]] = min(w[c[j]][c[k]], (c[k] - c[j]) * t[i]);
                        break;
                    }
                }
                SPFA(0);
            }
            if(d[k] != INF) printf("%d
    ", d[k]);
            else printf("IMPOSSIBLE
    ");
        }
        return 0;
    }
    View Code
  • 相关阅读:
    HDOJ -- 4632 区间DP
    leetcode230- Kth Smallest Element in a BST- medium
    leetcode240- Search a 2D Matrix II- medium
    leetcode369- Plus One Linked List- medium
    leetcode276- Paint Fence- easy
    leetcode54- Spiral Matrix- medium
    leetcode264- Ugly Number II- medium
    leetcode263- Ugly Number- easy
    leetcode616- Add Bold Tag in String- medium
    leetcode289- Game of Life- medium
  • 原文地址:https://www.cnblogs.com/sunus/p/4841879.html
Copyright © 2020-2023  润新知