1. 程式人生 > >poj 2253 Frogger dijkstra 0ms

poj 2253 Frogger dijkstra 0ms

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<stack>
#define N 200000
#include<math.h>
#include<queue>
#include<vector>
#define ll long long
#define inf 0x7fffffffffffff
#define maxn 300
using namespace std;
struct node {
int x,y;
};
node point[maxn];
double  jump_range[maxn];
int t;
bool vis[maxn];
double distance_my(int a,int b){///   點a點b距離
return sqrt((point[a].x-point[b].x)*(point[a].x-point[b].x)+(point[a].y-point[b].y)*(point[a].y-point[b].y));
}
void dijkstra(){
for(int i=1;i<=t;i++){
    jump_range[i]=inf;
}
jump_range[1]=0;
memset(vis,false,sizeof(vis));
for(int k=0;k<t;k++){
    double m=inf;
    int u;
    for(int j=1;j<=t;j++){
        if(!vis[j]&&m>jump_range[j]){
            m=jump_range[j];
            u=j;
        }
    }
    vis[u]=1;
    for(int v=1;v<=t;v++){///  更新每點的最短跳躍距離
        if(jump_range[v]>jump_range[u]&&jump_range[v]>distance_my(u,v)){
            jump_range[v]=jump_range[u]>distance_my(u,v)?jump_range[u]:distance_my(u,v);
        }
    }
}
}
int main(){
//freopen("input2.txt","r",stdin);
int cas=1;
while(scanf("%d",&t)&&t){
    for(int i=1;i<=t;i++){
        cin>>point[i].x>>point[i].y;
    }
    dijkstra();
printf("Scenario #%d\nFrog distance = %.3f\n\n",cas++,jump_range[2]);
}
return 0;
}