Uva 10801 最短路,搜索

A skyscraper has no more than 100 floors, numbered from 0 to 99. It has n (1 ≤ n ≤ 5) elevatorswhich travel up and down at (possibly) different speeds. For each i in {1, 2, . . . n}, elevator numberi 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 accessibleby an elevator.You are on floor 0 and would like to get to floor k as quickly as possible. Assume that you do notneed to wait to board the first elevator you step into and (for simplicity) the operation of switching anelevator on some floor always takes exactly a minute. Of course, both elevators have to stop at thatfloor. You are forbiden from using the staircase. No one else is in the elevator with you, so you don’thave to stop if you don’t want to. Calculate the minimum number of seconds required to get from floor0 to floor k (passing floor k while inside an elevator that does not stop there does not count as “gettingto floor k”).InputThe 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 visitedby elevator number 1, the next one will list the floors visited by elevator number 2, etc.OutputFor each test case, output one number on a line by itself - the minimum number of seconds required toget to floor k from floor 0. If it is impossible to do, print ‘IMPOSSIBLE’ instead.Explanation of examplesIn the first example, take elevator 1 to floor 13 (130 seconds), wait 60 seconds to switch to elevator2 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 Input2 3010 50 1 3 5 7 9 11 13 15 20 994 13 15 19 20 25 302 3010 10 5 10 12 14 20 25 302 4 6 8 10 12 14 22 25 28 293 5010 50 1000 10 30 400 20 300 20 501 120 2 4 6 8 10Sample Output2752853920IMPOSSIBLE


题目大意:有n个电梯,每个电梯一层的时间分别是T1,T2,T3....,从2到n+2行分别为所能到达的层,从0层坐电梯,每次切换电梯需要等60s问到达k层的最短时间是多少,如果不能到达输出IMPOSSIBLE。

贴一发搜索的代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<string>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<cstdlib>
using namespace std;
typedef long long  ll;
int vis[110],n,flo,dis[110],have[6][110];
int v[6],t[110];
struct CNode
{
	int num,fl;
	int t;
}a,b;
bool operator < ( const CNode & d1, const CNode & d2 ) {
	return d1.t > d2.t;
}
int bfs(){
    priority_queue<CNode>qu;
    a.t=0;
    a.num=0;
    a.fl=0;
    qu.push(a);
    while(!qu.empty()){
        a=qu.top();
        qu.pop();
        if(a.fl==flo){
            return a.t;
        }
        vis[a.fl]=1;
        if(!a.num){
            for(int i=1;i<=n;i++){
                if(have[i][0]){
                    b.fl=0;
                    b.num=i;
                    b.t=0;
                    qu.push(b);
                }
            }
        }
        else {
            for(int i=1;i<=n;i++){
                if(i==a.num){
                    for(int j=0;j<=100;j++){
                        if(!vis[j]&&have[i][j]){
                            b.num=i;
                            b.t=a.t+abs(a.fl-j)*v[i];
                            b.fl=j;
                            if(t[j]>b.t){//没有这句会超时
                                qu.push(b);
                                t[j]=b.t;
                            }
                        }
                    }
                }
                else {
                    for(int j=0;j<=100;j++){
                        if(!vis[j]&&have[i][j]&&have[i][a.fl]){
                            b.num=i;
                            b.t=a.t+abs(a.fl-j)*v[i]+60;
                            b.fl=j;
                            if(t[j]>b.t){
                                qu.push(b);
                                t[j]=b.t;
                            }
                        }
                    }
                }
            }
        }
    }
    return -1;
}
int main() {
    while(cin>>n>>flo) {
        for(int i=1; i<=n; i++){
            scanf("%d",v+i);
        }
        char c=1;
        getchar();
        memset(have,0,sizeof(have));
        for(int i=1;i<=n;i++){
            int h;
            while(1){
                scanf("%d%c",&h,&c);
                have[i][h]=1;
                if(c=='\n')break;
            }
        }
        memset(vis,0,sizeof(vis));
        for(int i=0;i<=110;i++)t[i]=2147483646;
        int t=bfs();
        if(t==-1)printf("IMPOSSIBLE\n");
        else cout<<t<<endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值