POJ2253Frogger题意难读懂 floyed求最短路中最长距离

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

Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fiona Frog who is sitting on another stone. He plans to visit her, but since the water is dirty and full of tourists' sunscreen, he wants to avoid swimming and instead reach her by jumping. 
Unfortunately Fiona's stone is out of his jump range. Therefore Freddy considers to use other stones as intermediate stops and reach her by a sequence of several small jumps. 
To execute a given sequence of jumps, a frog's jump range obviously must be at least as long as the longest jump occuring in the sequence. 

//一段距离中的最长值
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. 

//也就是最短路径中的最大长度值

You are given the coordinates of Freddy's stone, Fiona's stone and all other stones in the lake. Your job is to compute the frog distance between Freddy's and Fiona's stone. 

#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

struct node
{
    int x;
    int y;
} a[210];
double d[210][210];

int main()
{
    int n;
    int i, j, k;
    int cnt = 0;
    while(cin >> n)
    {
        if(n == 0)  break;
        cnt++;
        for(i = 0; i < n; i++)
        {
            scanf("%d %d", &a[i].x, &a[i].y);
        }
        for(i = 0; i < n; i++)
        {
            for(j = 0; j < n; j++)
            {
                d[i][j] = d[j][i] = sqrt((double)(a[i].x-a[j].x)*(a[i].x-a[j].x)+(double)(a[i].y-a[j].y)*(a[i].y-a[j].y));
            }
        }
        for(k = 0; k < n; k++)
        {
            for(i = 0; i < n; i++)
            {
                for(j = 0; j < n; j++)
                {
                    if(d[i][j] > max(d[i][k],d[k][j]))
                    {
                        d[i][j] = max(d[i][k],d[k][j]);
                    }
                }//A到B多条路径中的最长边中的最短距离
            }
        }
        printf("Scenario #%d\nFrog Distance = %.3f\n\n", cnt, d[0][1]);
    }

    return 0;
}
/*
题意:给出一个无向图,求一条1~2的路径使得路径上的最大边权最小.
分析:floyd变形,将更新距离的过程改为取最大值即可.
*/
#include
#include
#include
#include
using namespace std;

const double DNF = 2000;
const int maxn = 210;

double w[maxn][maxn];
int n;

struct Point
{
    double x,y;
} p[maxn];

double dist(Point a, Point b)
{
    return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
}

void floyd()
{
    for(int k = 1; k <= n; k++)
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= n; j++)
                w[i][j] = min(w[i][j], max(w[i][k], w[k][j])); //更新i——j路径上最小的最大边权
}

int main()
{
    int kcase = 0;
    while(scanf("%d", &n) != EOF)
    {
        if(n == 0) break;
        for(int i = 1; i <= n; i++)
        {
            scanf("%lf%lf", &p[i].x, &p[i].y);
        }

        for(int i = 1; i <= n; i++)
        {
            w[i][i] = 0;
            for(int j = i+1; j <= n; j++)
            {
                w[i][j] = dist(p[i],p[j]);
                w[j][i] = dist(p[i],p[j]);
            }
        }

        floyd();
        printf("Scenario #%d\n", ++kcase);
        printf("Frog Distance = %.3lf\n\n", w[1][2]);
    }
}

 

你可能感兴趣的:(ACM最短路Floyed)