POJ 2253 Floyd 算法

#include
#include
#include
#include
#define MAX 205
#define MAXN 40005
using namespace std;
struct Stone{
    int x;
    int y;
};
double map[MAX][MAX];
Stone rock[MAXN];
int main()
{
    int cases=0;
    int n,i,j,k;
    while(scanf("%d",&n)&&n!=0){
        memset(rock,0,sizeof(rock));
        for(i=1;i<=n;i++)
            scanf("%d%d",&rock[i].x ,&rock[i].y );
        for(i=1;i<=n;i++)
            for(j=i;j<=n;j++)
            {
                map[i][j]=map[j][i]=sqrt((double)((rock[i].x -rock[j].x)*(rock[i].x -rock[j].x)+(rock[i].y -rock[j].y)*(rock[i].y -rock[j].y)));    
            } 
        for(k=1;k<=n;k++)
        {
            for(i=1;i<=n;i++)
            {
                for(j=1;j<=n;j++)
                {
                    map[i][j]=min(map[i][j],max(map[i][k],map[k][j]));
                }
            }
        }
        printf("Scenario #%d\n",++cases);
        printf("Frog Distance = %.3f\n\n",map[1][2]);           
    }
}

运行时间 110MS
而Kruscal 16MS

可见Floyd 一个缺点就是费时,毕竟n3的算法

你可能感兴趣的:(随笔)