A strange lift(Dijistra优化)

A strange lift

Problem Description
There is a strange lift.The lift can stop can at every floor as you want, and there is a number Ki(0 <= Ki <= N) on every floor.The lift have just two buttons: up and down.When you at floor i,if you press the button "UP" , you will go up Ki floor,i.e,you will go to the i+Ki th floor,as the same, if you press the button "DOWN" , you will go down Ki floor,i.e,you will go to the i-Ki th floor. Of course, the lift can't go up high than N,and can't go down lower than 1. For example, there is a buliding with 5 floors, and k1 = 3, k2 = 3,k3 = 1,k4 = 2, k5 = 5.Begining from the 1 st floor,you can press the button "UP", and you'll go up to the 4 th floor,and if you press the button "DOWN", the lift can't do it, because it can't go down to the -2 th floor,as you know ,the -2 th floor isn't exist.
Here comes the problem: when you are on floor A,and you want to go to floor B,how many times at least he has to press the button "UP" or "DOWN"?
 

 

Input
The input consists of several test cases.,Each test case contains two lines.
The first line contains three integers N ,A,B( 1 <= N,A,B <= 200) which describe above,The second line consist N integers k1,k2,....kn.
A single 0 indicate the end of the input.
 

 

Output
For each case of the input output a interger, the least times you have to press the button when you on floor A,and you want to go to floor B.If you can't reach floor B,printf "-1".
 

 

Sample Input
5 1 5
3 3 1 2 5
0
 

 

Sample Output
3
 
这题一开始用基本的dijistra老是时间超限,就试了优化了的,运行时间就变15ms了,快多了。
 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 #include <vector>
 5 #include <queue>
 6 using namespace std;
 7 const int N = 220;
 8 const int INF = 1e7;
 9 int d[N], n, a, b;
10 
11 struct edge{
12     int to, cost;
13 };
14 vector<edge> g[N];
15 typedef pair<int,int> P;
16 void dijistra(int s){
17     priority_queue<P,vector<P>,greater<P> > que;
18     d[a] = 0;
19     que.push(P(0,s));
20     while(!que.empty()){
21         P p = que.top();que.pop();
22         int v = p.second;
23         if(d[v] < p.first) continue;
24         for(int i = 0; i < g[v].size(); i ++){
25             edge e = g[v][i];
26             if(d[e.to] > d[v] + e.cost){
27                 d[e.to] = d[v] + e.cost;
28                 que.push(P(d[e.to],e.to));
29             }
30         }
31     }
32 }
33 
34 
35 int main(){
36     while(~scanf("%d",&n)&&n){
37         for(int i = 0; i < N; i ++){
38             d[i] = INF;
39             g[i].clear();
40         }
41         scanf("%d %d",&a,&b);
42         for(int i = 1; i <= n; i ++){
43             int tmp;
44             edge e;
45             scanf("%d",&tmp);
46             if(i-tmp > 0){
47                 e.to = i-tmp;
48                 e.cost = 1;
49                 g[i].push_back(e);
50             }
51             if(i+tmp <= n){
52                 e.to = i + tmp;
53                 e.cost = 1;
54                 g[i].push_back(e);
55             }
56         }
57         dijistra(a);
58         printf("%d\n",(d[b]==INF)?-1:d[b]);
59     }
60     return 0;
61 }

 

posted @ 2017-05-06 16:50  starry_sky  阅读(159)  评论(0编辑  收藏  举报