Frogger--poj2253

http://poj.org/problem?id=2253

题意:The frog distance (humans also call it minimax distance) between two stones therefore is defined as the minimum necessary jump range over all possible paths between the two stones.
这句话很重要,题中求得是青蛙一次至少跳多远才能成功到达对方所在的地方;即求所有路径中每条路径中的最大距离的最小值(求一条1~2的路径使得路径上的最大边权最小)

floyd中更新距离改成更新最小的最大边权;

#include<iostream>
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#define INF 0xfffffff
#define N 1100
using namespace std;
int n;
double maps[N][N];

struct node
{
    int x,y;
}a[N];
void Floyd()
{
    int i,j,k;
    for(k=1;k<=n;k++)
    {
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=n;j++)
            {
                maps[i][j]=min(maps[i][j],max(maps[i][k],maps[k][j]));
            }
        }
    }
}

int main()
{
    int i,j,k,x,y,t=1;

    while(scanf("%d",&n),n)
    {
        memset(a,0,sizeof(a));

        k=1;

        for(i=1;i<=n;i++)
        {
            scanf("%d%d",&x,&y);

            for(j=1;j<=k;j++)
            {
                double d=sqrt((x-a[j].x)*(x-a[j].x)+(y-a[j].y)*(y-a[j].y));

                maps[i][j]=maps[j][i]=d;
            }

            a[k].x=x,a[k].y=y,k++;
        }
        for(i=1;i<=n;i++)

            maps[i][i]=0;

        Floyd();

        printf("Scenario #%d\nFrog Distance = %.3f\n\n",t++,maps[1][2]);
    }
    return 0;
}
View Code

 用Dij算法就相当与prim;

dist[i] 代表i点之前的那个点到i的最短距离;求最短中的最长;

#include<iostream>
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#define INF 0xfffffff
#define N 1100
using namespace std;
int n,vis[N];
double maps[N][N],dist[N],ans;
struct node
{
    int x,y;
}a[N];
void Dij()
{
    int i,j,index;
    dist[1]=0;
    for(i=1;i<=n;i++)
    {
        double Min=INF;
        for(j=1;j<=n;j++)
        {
            if(vis[j]==0&&Min>=dist[j])
                Min=dist[j],index=j;
        }
        vis[index]=1;
        if(ans<dist[index]&&dist[index]!=INF)
            ans=dist[index];
        if(index==2)
            return ;
        for(j=1;j<=n;j++)
        {
            if(vis[j]==0)
                dist[j]=min(dist[j],maps[index][j]);
        }
    }
}
int main()
{
    int i,j,k,x,y,t=1;
    while(scanf("%d",&n),n)
    {
        memset(vis,0, sizeof(vis));
        memset(a,0,sizeof(a));
        for(i=0;i<=n;i++)
            dist[i]=INF;
        k=1;
        for(i=1;i<=n;i++)
        {
            scanf("%d%d",&x,&y);
            for(j=1;j<=k;j++)
            {
                double d=sqrt((x-a[j].x)*(x-a[j].x)+(y-a[j].y)*(y-a[j].y));
                maps[i][j]=maps[j][i]=d;
            }
            a[k].x=x,a[k].y=y,k++;
        }
        for(i=1;i<=n;i++)
            maps[i][i]=0;
        ans=0;
        Dij();
        printf("Scenario #%d\nFrog Distance = %.3f\n\n",t++,ans);
    }
    return 0;
}
View Code

 

posted @ 2015-07-22 13:28  西瓜不懂柠檬的酸  Views(310)  Comments(0Edit  收藏  举报
levels of contents