问题描述
- HDU-1548超时怎么解决? 5C
-
#include<iostream>#include<cstdio>#include<queue>#include<stdlib.h>int sum=0;int NAB;typedef struct lift{ int timeFLOOR; 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]={01-1}k1[202]={0}; int i; for(i=1;i<=N;i++) { scanf(""%d""&k[i]); } int t; t=BFS(kk2k1); printf(""%dn""t); } return 0;}int BFS(int k[]int k2[]int k1[]){ priority_queue<floor> que; floor startnextcur; 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;}我觉得是循环那个地方超时了,但是没有想到怎么解决
解决方案
http://www.cnblogs.com/wally/archive/2013/01/30/2882681.html
http://blog.csdn.net/libin56842/article/details/16949981
时间: 2024-12-30 04:20:24