标签:
HDU1548:A strange lift Dijkstra
Description
Input
Output
Sample Input
Sample Output
1 #include<stdio.h> 2 #include<iostream> 3 #include<string.h> 4 #define max 0x3f3f3f3f 5 6 using namespace std; 7 8 int main() 9 { 10 int n,a,b,x; 11 int i,j,k,min; 12 int floor[201][201],f[201],used[201]; 13 while(scanf("%d",&n)&&n) 14 { 15 16 memset(used,0,sizeof(used)); 17 scanf("%d %d",&a,&b); 18 19 for(i=1;i<=n;i++) 20 { 21 for(j=1;j<=n;j++) 22 floor[i][j]=max; 23 24 scanf("%d",&x);///下面记录该楼层可到达的楼层,记为1 25 if(i+x<=n) 26 floor[i][i+x]=1; 27 if(i-x>=1) 28 floor[i][i-x]=1; 29 } 30 31 for(i=1;i<=n;i++) 32 f[i]=floor[a][i]; 33 34 f[a]=0;//初值是起点到每个点的距离! 35 36 for(i=1;i<=n;i++) 37 { 38 min=max; 39 for(j=1;j<=n;j++) 40 if(!used[j]&&f[j]<min) 41 { 42 min=f[j];k=j; 43 } 44 45 if(min==max) 46 break; 47 used[k]=1; 48 49 for(j=1;j<=n;j++) 50 if(!used[j]&&f[j]>floor[k][j]+f[k]) 51 f[j]=floor[k][j]+f[k]; 52 } 53 54 if(f[b]==max) 55 printf("-1\n"); 56 else 57 printf("%d\n",f[b]); 58 } 59 return 0; 60 }
标签:
原文地址:http://www.cnblogs.com/moqitianliang/p/4685873.html