HDU 1548 A strange lift

来源:互联网 发布:伤害跳跃网络吧 编辑:程序博客网 时间:2024/05/20 06:51
http://acm.hdu.edu.cn/showproblem.php?pid=1548

A strange lift

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13222    Accepted Submission(s): 5065


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 53 3 1 2 50
 

Sample Output
3
 

Recommend
8600   |   We have carefully selected several similar problems for you:  1385 1242 1142 1217 1253 
这道题并不难,注意是单向图还有建图

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<iomanip>
#include<list>
#include<deque>
#include<map>
#include <stdio.h>
#include <queue>

#define maxn 10000+5
#define ull unsigned long long
#define ll long long
#define reP(i,n) for(i=1;i<=n;i++)
#define rep(i,n) for(i=0;i<n;i++)
#define cle(a) memset(a,0,sizeof(a))
#define mod 90001
#define PI 3.141592657
#define INF 99999
const ull inf = 1LL << 61;
const double eps=1e-5;

using namespace std;

bool cmp(int a,int b){
return a>b;
}
int edge[205][205];
int lowcost[205],vis[205];
int n,a,b;
void Dj(int a)
{
cle(vis);
vis[a]=1;
for(int i=1;i<=n;i++)
lowcost[i]=edge[a][i];
int Min,k;
while(1)
{
Min=INF;
for(int i=1;i<=n;i++)
if(lowcost[i]<Min&&!vis[i])
{
k=i;Min=lowcost[i];
}
if(Min==INF)break;
vis[k]=1;
for(int j=1;j<=n;j++)
{
if(lowcost[j]>lowcost[k]+edge[k][j])
lowcost[j]=lowcost[k]+edge[k][j];
}
}
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
while(cin>>n&&n)
{
cin>>a>>b;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
edge[i][j]=INF;
// edge[j][i]=INF;
}
edge[i][i]=0;
}
int ki;
for(int i=1;i<=n;i++)
{
cin>>ki;
if(i+ki<=n)
{
if(edge[i][ki+i]>1) edge[i][i+ki]=1;
//if(edge[i+ki][i]>1)edge[i+ki][i]=1;
}
if(i-ki>=1)
{
if(edge[i][i-ki]>1)edge[i][i-ki]=1;
//if(edge[i-ki][i]>1)edge[i-ki][i]=1;
}
}
Dj(a);
if(lowcost[b]!=INF)cout<<lowcost[b]<<endl;
else cout<<-1<<endl;
}
return 0;
}


刚开始一直提交双向图的dijkstra  错了!
0 0
原创粉丝点击