uva 10801 - Lift Hopping

点击打开链接uva 10801

思路:最短路+Dijkstra

分析:
         1 有n个电梯,电梯可以到达的层数是一定的,那么我们就把楼层看成是图上的点,那么我们就可以得到的哪些点是有连通的。
         2 又由于有多个电梯,所以x->y可能有多种方式,所以这就类似于重边问题那么我们要选择边权值最小的最为最后的边权值。
         3 接下来图建好以后就是考虑怎么Dijkstra,由于要交换乘坐电梯,那么我们就自然的想到了图论中的换边(松弛步),那么我们就把换乘电梯看成是在换边,就是说只要做松弛步就是在换成电梯,那么这个时候就要加上等电梯的时间60s。
         4 这里有个地方就是由于源点是会做松弛步的,但是源点是不用加上60s的,所以最后输出的时候要特判一下。如果k为0,直接输出0;如果k部位0,那么最后的输出要减去60.

代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
#define MAXN 110
#define INF 0xFFFFFFF

int n , k , pos;
int value[MAXN][MAXN];
int t[6];
int dis[MAXN];
int vis[MAXN];
int num[10010];

/*初始化变量*/
void init(){
     memset(t , 0 , sizeof(t));
     memset(vis , 0 , sizeof(vis));
     dis[0] = 0;
     for(int i = 1 ; i < 100 ; i++)
        dis[i] = INF;
     for(int i = 0 ; i <100 ; i++){
        for(int j = 0 ; j < 100 ; j++)
           value[i][j] = INF;
     }
}

/*建立无向图*/
void build_Grap(int x){
     for(int i = 0 ; i < pos; i++){
        for(int j = i ; j < pos ; j++){
           int tmp = abs(num[j]-num[i])*t[x];
           if(value[num[i]][num[j]] > (abs(num[i]-num[j]))*t[x])
             value[num[i]][num[j]] = value[num[j]][num[i]] = tmp;
        }
     }
}

/*Dijkstra算法*/
void Dijkstra(){
     for(int i = 0 ; i < 100 ; i++){
        pos = -1;
        for(int j = 0 ; j < 100 ; j++){
           if(!vis[j] && (pos == -1 || dis[j] < dis[pos]))
             pos = j;
        }
        vis[pos] = 1;
        for(int j = 0 ; j < 100 ; j++){
           if(!vis[j]){
                if(dis[j] > dis[pos] + value[pos][j] + 60)/*这里注意一下要加上60*/
                  dis[j] = dis[pos] + value[pos][j] + 60;
           }
        }
     }
     /*输出特判*/
     if(dis[k] != INF){
        if(k)
          printf("%d\n" , dis[k]-60);
        else
          printf("0\n");
    }
    else
       printf("IMPOSSIBLE\n");
}

int main(){
    //freopen("input.txt" , "r" , stdin);
    int tmp;
    char ch;
    while(scanf("%d%d" , &n , &k) != EOF){
        init();
        for(int i = 1 ; i <= n ; i++)
          scanf("%d" , &t[i]);
        for(int i = 1 ; i <= n ; i++){
           memset(num , 0 , sizeof(num));
           pos = 0;
           /*输入问题用getchar()来判断*/
           do{
             scanf("%d" , &num[pos++]);
           }while(getchar() != '\n');
           build_Grap(i);
        }
        Dijkstra();
    }
    return 0;
}
时间: 2024-05-06 05:23:55

uva 10801 - Lift Hopping的相关文章

uva 10801 - Lift Hopping(最短路Dijkstra)

/* 题目大意: 就是一幢大厦中有0-99的楼层, 然后有1-5个电梯!每个电梯有一定的上升或下降速度和楼层的停止的位置! 问从第0层楼到第k层最少经过多长时间到达! 思路:明显的Dijkstra ,在建图的时候u->v可能有多个电梯到达,取时间最少的当作路径的权值! 如果我们发现 d[i] > d[j] + map[j][i] + 60, 那么说明从第0层到达第 i 层的时间大于从第j层 转移到其他电梯然后到达第 i 层的时间,那么就更新d[i]的值! */ #include<iost

UVa 10801:Lift Hopping(Dijkstra, SPFA最短路)

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1742 题目: Problem ? Lift Hopping Time Limit: 1 second Ted the bellhop: "I'm coming up and if there isn't a dead body by the

最短路专题【完结】

第一题 hdu 1317 XYZZY 点击打开hdu 1317 思路: 1 题目的图是一个有向图,并且可能存在环.第一个点的能量值为100,边的权值利用能量大小,例如2点为-60,如果1->2那么value[1][2] = -602 题目明确指出如果是要win的话,那么必须是经过的每条边都要大于0.那么我们只要把那些经过松弛操作后的点大于0的入队即可,小于等于0的点肯定不会出现在最终的路径上.3 如果存在正环的话,那么就有能量值无限大,那么这个时候只要判断这个点能否到达n4 判断是否是有环还是五

UVa 10034:Freckles (最小生成树模板题)

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=975 题目: Problem A: Freckles In an episode of the Dick Van Dyke show, little Richie connects the freckles on his Dad's back to fo

UVa 10940 Throwing cards away II:约瑟夫问题

10940 - Throwing cards away II Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=1881 Given is an ordered deck of n cards numbered 1 to n with card 1 at th

UVa 10602

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1543 类型:贪心 原题: Company Macrohard has released it's new version of editor Nottoobad, which can understand a few voice commands.

UVa 10392 Factoring Large Numbers:素因子分解

10392 - Factoring Large Numbers Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=100&page=show_problem&problem=1333 One of the central ideas behind much cryptography is that factoring

UVa 10182 Bee Maja:规律&amp;amp;O(1)算法

10182 - Bee Maja Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1123 Maja is a bee. She lives in a bee hive with thousands of other bees. This bee hive c

算法题之UVA 763

Fibinary Numbers The standard interpretation of the binary number 1010 is 8 + 2 = 10. An alternate way to view the sequence ``1010'' is to use Fibonacci numbers as bases instead of powers of two. For this problem, the terms of the Fibonacci sequence