poj 2253Frogger

思路:

Dijkstra

代码:

#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
using namespace std;
struct infor{
    int x;
    int y;
};
int t,n=0;
infor stone[202];
double d(int x,int y)
{
    return sqrt(x*x+y*y);
}
double dis[201];
double map[202][202];
bool visit[202];
using namespace std;
void dijkstra()
{
    while(true){
        double m=1000000.6;
        int p=0;
        for(int i=0;i<t;i++){
            if(!visit[i]&&dis[i]<m){
                m=dis[i];
                p=i;
            }
        }
        visit[p]=1;
        if(p==1)
            break;
        for(int i=0;i<t;i++){
            double n=max(map[p][i],dis[p]);
            dis[i]=min(n,dis[i]);
        }
    }
}

int main()
{
    while(cin>>t&&t){
        memset(visit,0,sizeof(visit));
        for(int i=0;i<t;i++){
            cin>>stone[i].x>>stone[i].y;
        }
        for(int i=0;i<t;i++){
            for(int j=0;j<t;j++){
                map[i][j]=d(stone[i].x-stone[j].x,stone[i].y-stone[j].y);
            }
        }
        for(int i=0;i<t;i++){
            dis[i]=map[0][i];
        }
        visit[0]=1;
        dijkstra();
        n++;
        printf("Scenario #%d\n",n);
        printf("Frog Distance = %.3f\n",dis[1]);
        cout<<endl;
    }
    cout<<endl;
    return 0;
}
View Code

 

posted @ 2013-11-08 17:07  Neptunes  阅读(197)  评论(0编辑  收藏  举报