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 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 floork").

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 T1T2,... 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.

Sample InputSample Output
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
275
285
3920
IMPOSSIBLE

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.


题目大意:

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

一个人从0层开始,目的地是k层, 现在要搭这些电梯,问最少需多少时间。


分析与总结:

最短路问题,增加了一些条件,即换乘时需要等60秒。 

这的关键在于转换,先把每个电梯能到达的层的所有起点与终点,所用的时间存到邻接矩阵中。如果某条路经已有,那么注意要保存所需时间最少的那个。

然后就是求最短路算法求出最少时间,用Dijkstra或者SPFA均可。

需要注意的是,起点是0的不要等待时间,所以特判,并且不要加上60。


代码:

#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
#include<utility>
using namespace std;

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

const int N   = 105;
const int INF = 1000000000;
int n, k, T[6], w[N][N], arr[N], d[N];
bool vis[N];



inline void read_graph(){
    for(int i=0; i<N; ++i){
        w[i][i] = INF;
        for(int j=i+1; j<N; ++j)
            w[i][j]=w[j][i]=INF;
    }
    for(int i=0; i<n; ++i)
        scanf("%d",&T[i]);
    
    char ch;
    for(int i=0; i<n; ++i){
        int pos=0;
        do{
            scanf("%d",&arr[pos++]);
        }while(getchar()!='\n');

        for(int j=0; j<pos; ++j){
            for(int k=j; k<pos; ++k){
                int tmp=abs(arr[j]-arr[k])*T[i];
                if(tmp<w[arr[j]][arr[k]]){
                    w[arr[j]][arr[k]] = w[arr[k]][arr[j]] = tmp;
                }
            }
        }
    }
}

inline void SPFA(int src){
    memset(vis, 0, sizeof(vis));
    for(int i=0; i<N; ++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<N; ++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);
                }
            }
        }
    }
}

inline bool Dijkstra(int src){
    for(int i=0; i<N; ++i) d[i] = INF;
    d[src] = 0;
    
    q.push(make_pair(d[src], src));
    while(!q.empty()){
        pii t=q.top(); q.pop();
        int u=t.second;
        if(t.first!=d[u]) continue;
        for(int v=0; v<N; ++v){
            if(u==0){
                if(d[v]>d[u]+w[u][v]){
                    d[v] = d[u]+w[u][v];
                    q.push(make_pair(d[v],v));
                }
            }
            else{
                if(d[v]>d[u]+w[u][v]+60){
                    d[v]=d[u]+w[u][v]+60;
                    q.push(make_pair(d[v],v));
                }
            }
        }
    }
}
int main(){
    while(~scanf("%d%d",&n,&k)){
        read_graph(); 
    //     SPFA(0);
         Dijkstra(0);
        if(d[k]!=INF) printf("%d\n", d[k]);
        else printf("IMPOSSIBLE\n");
    }
    return 0;
}

——  生命的意义,在于赋予它意义。

          
     原创 http://blog.csdn.net/shuangde800 , By   D_Double  (转载请标明)







  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值