#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
const int maxn=202;
const int INF=1<<29;
int n,st,ed,k[maxn],map[maxn][maxn],dis[maxn],vis[maxn];
struct Node
{
int v,next,w;
bool operator < (const Node &a) const
{
return w > a.w;
}
} Edge[2000002],t1,t2;
int dijstra(int st,int ed)
{
priority_queue<Node>q;
for(int i=1; i<=n; i++)
{
dis[i]=INF;
if(map[st][i]<dis[i])
{
dis[i] = map[st][i];
t1.w = dis[i];
t1.v = i;
q.push(t1);
}
}
dis[st]=0;//此句没加错了2次,囧~~~
vis[st] = 1;
while(!q.empty())
{
t1 = q.top();
q.pop();
int u = t1.v;
if(vis[u]) continue;
vis[u] = 1;
for(int v=1; v<=n; v++)
{
if(!vis[v])
{
if(dis[v]>dis[u]+map[u][v])
{
dis[v] =dis[u]+map[u][v];
t2.v = v;
t2.w = dis[v];
q.push(t2);
}
}
}
}
if(dis[ed]>=INF)return -1;
return dis[ed];
}
int main()
{
while(scanf("%d",&n),n)
{
scanf("%d%d",&st,&ed);
int i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
map[i][j]=INF;
}
map[i][i]=0;
}
memset(vis,0,sizeof(vis));
for(i=1;i<=n;i++)
{
scanf("%d",&k[i]);
if(i-k[i]>=1)map[i][i-k[i]]=1;
if(i+k[i]<=n)map[i][i+k[i]]=1;
}
printf("%d\n",dijstra(st,ed));
}
return 0;
}
hdu 1548 简单 dijstra
最新推荐文章于 2019-12-06 00:45:47 发布