2 qwezhaohaihong 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
caozhy   Ds   Rxr 2016.02.25 17:17
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!