算是最短路径的一种变形。也就是松弛操作中那个运算变化了,这一点还需要通过做题慢慢理解。 #include<stdio.h> #include<math.h> const double eps = 1e-8; #define INF 999999999 struct stone { int x,y; } stones[201]; int main(void) { int flag[201]; double dist[201]; double mat[201][201]; int start=1,i,j,k; int n,now; double t,min; while( scanf("%d",&n) && n ) { for( i = 1; i <= n; i++ ) { scanf("%d%d",&stones[i].x,&stones[i].y); dist[i] = INF; flag[i] = 0; } for( i = 1; i <= n; i++ ) { for( j = 1; j <= n; j++ ) { if( i == j ) { mat[i][j] = INF; continue; } mat[i][j] = sqrt( (stones[i].x-stones[j].x)*(stones[i].x-stones[j].x)+ (stones[i].y-stones[j].y)*(stones[i].y-stones[j].y) ); } } dist[1] = 0; flag[now = 1] = 1; for( i = 1; i < n; i++ ) { for( j = 1; j <= n; j++ ) { t = dist[now]>(mat[now][j]+eps)?dist[now]:mat[now][j]; if( t + eps < dist[j] ) dist[j] = t; } for( j = 1,min = INF; j <= n; j++ ) if( flag[j] == 0 && dist[j] + eps < min ) min = dist[now = j]; flag[now] = 1; } printf("Scenario #%d/n",start++); printf("Frog Distance = %.3f/n/n",dist[2]); } return 0; }