qwezhaohaihong 于 2016.02.25 17:16 提问

HDU-1548超时怎么解决？ 5C
#include<iostream>
#include<cstdio>
#include<queue>
#include<stdlib.h>
int sum=0;
int N,A,B;
typedef struct lift
{
int time,FLOOR;
bool operator < (const lift &a) const{
return time>a.time;
}
}floor;
int BFS(int k[],int k2[],int k1[]);
using namespace std;
int main()
{
while(scanf("%d",&N)!=EOF)
{
if(N==0){break;}
scanf("%d%d",&A,&B);
int k[201],k2[3]={0,1,-1},k1[202]={0};
int i;
for(i=1;i<=N;i++)
{
scanf("%d",&k[i]);
}
int t;
t=BFS(k,k2,k1);
printf("%d\n",t);

}
return 0;

}
int BFS(int k[],int k2[],int k1[])
{
priority_queue<floor> que;
floor start,next,cur;
start.FLOOR=A;
start.time=0;
k1[A]=1;
que.push(start);
while(!que.empty())
{
cur=que.top();
que.pop();
int z;
for(z=1;z<=2;z++)
{
next.FLOOR=cur.FLOOR+k[cur.FLOOR]*k2[z];
next.time=cur.time+1;

if((next.FLOOR>N)||(next.FLOOR<1)){continue;}

if(k1[next.FLOOR]==1){continue;}

k1[next.FLOOR]=1;//1代表已经走过；

if(next.FLOOR==B){return next.time;}

else{ que.push(next); }

}

}
return -1;

}

1个回答

caozhy      2016.02.25 17:17