UVA - 10801Lift Hopping(Dijkstra)

题目链接

题目大意:有n个电梯,每个电梯有制定停靠的楼层,如果你从一个电梯换乘另一个电梯需要等待60s。一开始你在0层,在0层的时候不需要等待。现在指定你要到的楼层,问你通过这n个电梯到达目标楼层的最快时间。

解题思路:这题是最短路算法,但是不容易转化,因为你需要先处理出可以停靠的任意两个楼层之间的最短时间,然后再转换成你从0层到目标层的最短路问题,之后的每次松弛需要加上等待时间,因为换乘电梯了。注意:0层的时候要特判,因为是不需要等待的。

代码:

#include <cstdio>
#include <cstring>
#include <queue>

using namespace std;

typedef pair<int, int>pii;
priority_queue<pii, vector<pii>, greater<pii> >q;

const int maxn = 10;
const int maxm = 100;
const int INF = 1e6;
const int wait = 60;

int t[maxn];
int floor[maxm];
int w[maxm][maxm];
int d[maxm];

void read_Graph (int n, int k) {

    int pos;
    char ch;

    for (int i = 0; i < maxm; i++)
        for (int j = i; j < maxm; j++)
            w[j][i] = w[i][j] = (i==j)? 0 : INF;

    for (int i = 0; i < n; i++)
        scanf ("%d", &t[i]);
    for (int i = 0; i < n; i++) {

        pos = 0;
        do {
            scanf ("%d%c", &floor[pos++], &ch);
        } while (ch != ‘\n‘);

        for (int j = 0; j < pos; j++)
            for (int x = j; x < pos; x++)
                if (w[floor[x]][floor[j]] > (floor[x] - floor[j]) * t[i])
                    w[floor[x]][floor[j]] = w[floor[j]][floor[x]] = (floor[x] - floor[j]) * t[i];
    }

}

int Dijkstra (int n, int k) {

    for (int i = 0; i < maxm; i++)
        d[i] = INF;

    d[0] = 0;

    pii u = make_pair(d[0], 0);
    q.push(u);

    while (!q.empty()) {

        u = q.top();
        q.pop();

        if (u.first != d[u.second]) continue;

        for (int i = 0; i < maxm; i++) {
            if (i == u.second) continue;

            if (u.second == 0) {

                if (d[i] > u.first + w[u.second][i])
                    d[i] = u.first + w[u.second][i];
                q.push(make_pair(d[i], i));

            } else if (d[i] > u.first + w[u.second][i] + wait) {

                    d[i] = u.first + w[u.second][i] + wait;
                    q.push(make_pair(d[i], i));
            }
        }
    }

    return d[k];
}

int main () {

    int n, k;

    while (scanf ("%d%d", &n, &k) != EOF) {
        read_Graph(n, k);
        int ans = Dijkstra(n, k);

        if (ans == INF)
            printf ("IMPOSSIBLE\n");
        else
            printf ("%d\n", ans);
    }
    return 0;
}
时间: 2024-08-11 05:44:01

UVA - 10801Lift Hopping(Dijkstra)的相关文章

【UVA】821-Page Hopping(Floyd)

模板题,求一个点到任何一点的距离,用Floyd就行了,结点不一定是从1 ~ n 的,所以需要记录结点的id 14063895 821 Page Hopping Accepted C++ 0.119 2014-08-19 10:00:27 #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<vector> #include<sta

最短路径算法——迪杰斯特拉算法(Dijkstra)

图结构中应用的最多的就是最短路径的查找了,关于最短路径查找的算法主要有两种:迪杰斯特拉算法(Dijkstra)和Floyd算法. 其中迪杰斯特拉算法(Dijkstra)实现如下: 原理就是不断寻找当前的最优解: void main() { int V[Max][Max]={0,8,32,Infinity,Infinity, 12,0,16,15,Infinity, Infinity,29,0,Infinity,13, Infinity,21,Infinity,0,7, Infinity,Infi

hdu 1874(Dijkstra )

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1874 畅通工程续 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 27692    Accepted Submission(s): 10019 Problem Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过路

UVA - 10534Wavio Sequence(LIS)

题目:UVA - 10534Wavio Sequence(LIS) 题目大意:给出N个数字,找出这样的序列:2 * n + 1个数字组成.前面的n + 1个数字单调递增,后面n + 1单调递减. 解题思路:从前往后找一遍LIS,再从后往前找一遍LIS.最后只要i这个位置的LIS的长度和LDS的长度取最小值.再*2 - 1就是这个波浪数字的长度.注意这里的求LIS要用nlog(n)的算法,而且这里的波浪数字的对称并不是要求i的LIS == LDS,而是只要求LIS和LDS最短的长度就行了,长的那个

poj1062昂贵的聘礼(Dijkstra**)

1 /* 2 题意: 物主有一个物品,价值为P,地位为L, 以及一系列的替代品Ti和该替代品所对应的"优惠"Vi 3 g[u][i] 表示的是u物品被i物品替换后的优惠价格!(u>0, i>0) 4 g[u][0]表示不用替换该物品的实际价格 ! 5 d[0]表示的是第一个物品经过一系列的物品替换之后的最少优惠价格! 6 7 思路:每当我们通过Dijkstra算法得到离源点(1)最近的距离的节点 p的时候(也就是1...pre[p], p)这条 8 路径上的物品互相替换后得

uva 725 Division(除法)暴力法!

uva 725  Division(除法) A - 暴力求解 Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description Write a program that finds and displays all pairs of 5-digit numbers that between them use the digits 0 through 9 once each, such that t

Uva 11889 - Benefit( 数论 )

Uva 11889 - Benefit( 数论 ) 题意: calculate the lowest integer B such that LCM(A, B) = C 分析: LCM(A,B) = C = A*B/GCD(A,B)C*GCD(A,B) = A*BC/A = B/GCD(A,B)如果C%A != 0 无解否则, 令t = C/AB = t * GCD(A,B) 即B 一定是 t 的整数倍从t开始枚举B #include <cstdio> typedef long long LL

UVA 10139 Factovisors(数论)

Factovisors The factorial function, n! is defined thus for n a non-negative integer: 0! = 1 n! = n * (n-1)! (n > 0) We say that a divides b if there exists an integer k such that k*a = b The input to your program consists of several lines, each conta

迪杰斯特拉(dijkstra)算法的简要理解和c语言实现(源码)

迪杰斯特拉(dijkstra)算法:求最短路径的算法,数据结构课程中学习的内容. 1 . 理解 算法思想::设G=(V,E)是一个带权有向图,把图中顶点集合V分成两组,第一组为已求出最短路径的顶点集合(用S表示,初始时S中只有一个源点,以后每求得一条最短路径 , 就将 加入到集合S中,直到全部顶点都加入到S中,算法就结束了),第二组为其余未确定最短路径的顶点集合(用U表示),按最短路径长度的递增次序依次把第二组的顶点加入S中.在加入的过程中,总保持从源点v到S中各顶点的最短路径长度不大于从源点v