avatar
fireworks99
keep hungry keep foolish

POJ 2253 Frogger

Description

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.

题目链接 http://poj.org/problem?id=2253

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.

Input

The input will contain one or more test cases. The first line of each test case will contain the number of stones n (2<=n<=200). The next n lines each contain two integers xi,yi (0 <= xi,yi <= 1000) representing the coordinates of stone #i. Stone #1 is Freddy’s stone, stone #2 is Fiona’s stone, the other n-2 stones are unoccupied. There’s a blank line following each test case. Input is terminated by a value of zero (0) for n.

Output

For each test case, print a line saying “Scenario #x” and a line saying “Frog Distance = y” where x is replaced by the test case number (they are numbered from 1) and y is replaced by the appropriate real number, printed to three decimals. Put a blank line after each test case, even after the last one.

Sample Input

2

0 0

3 4

3

17 4

19 4

18 5

0

Sample Output

Scenario #1

Frog Distance = 5.000

Scenario #2

Frog Distance = 1.414

题意

此青蛙想到彼青蛙那里去,期间有许多石头,此青蛙可以通过这些石头跳过去。问至少要跳的最大距离,即所有路径上石头间的最大距离的最小值。

最短路变形

最小化最大值:

两条路:

  1. 当前直达

  2. 中转

    择优(最远“跳距”最小的那条)

这里有个相反的:最大化最小值: https://fireworks99.github.io/2019/03/29/POJ-1797-Heavy-Transportation/

code of spfa

#include <cmath>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int inf = 0x3f3f3f3f;

int n;
bool vis[305];
double dis[305][305];
double ans[305];///ans[i]存:起点到i点所有路的所有路段(经最短化处理)中的最长边

struct node
{
    double x, y;
} a[305];


void spfa(int start)
{
    memset(vis, 0, sizeof(vis));
    for(int i = 1; i <= n; ++i)
        ans[i] = inf;///inf是int型的,不能memset
    ans[start] = 0;
    vis[start] = 1;
    queue<int> q;
    q.push(start);
    while(q.size())
    {
        int first = q.front();///中转站
        q.pop();
        vis[first] = 0;
        for(int i = 1; i <= n; ++i)
            if(ans[i] > max(ans[first], dis[first][i]))
            {
                ans[i] = max(ans[first], dis[first][i]);
                if(!vis[i])
                {
                    q.push(i);
                    vis[i] = 1;
                }
            }

    }
}

int main()
{
    int cnt = 0;
    while(cin >> n)
    {
        if(n == 0)
            break;
        cnt++;
        for(int i = 1; i <= n; ++i)
            cin >> a[i].x >> a[i].y;
        for(int i = 1; i <= n; ++i)
            for(int j = i + 1; j <= n; ++j)
                dis[i][j] = dis[j][i] = sqrt((a[i].x - a[j].x) * (a[i].x - a[j].x) + (a[i].y - a[j].y) * (a[i].y - a[j].y));
        spfa(1);
        printf("Scenario #%d\nFrog Distance = %.3f\n\n", cnt, ans[2]);
    }
    return 0;
}

Code of Dijkstra

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int inf = 0x3f3f3f3f;

int n;
bool vis[305];
double dis[305][305];
double ans[305];///ans[i]存:起点到i点所有路的所有路段(经最短化处理)中的最长边

struct node
{
    double x, y;
} a[305];

void dijkstra(int start)
{
    memset(vis, 0, sizeof(vis));
    for(int i = 1; i <= n; ++i)
        ans[i] = inf;///inf是int型的,不能memset
    ans[start] = 0;
    for(int i = 1; i <= n; ++i)
    {
        int mmin = inf;
        int pos;
        for(int j = 1; j <= n; ++j)
            if(!vis[j] && ans[j] < mmin)
            {
                pos = j;///距离i点最近点的标号
                mmin = ans[j];///与i点相连点中、离i最近的点、距离i的距离
            }
        vis[pos] = 1;
        for(int j = 1; j <= n; ++j)
            ans[j] = min(ans[j], max(ans[pos], dis[pos][j]));
    }
}

int main()
{
    int cnt = 0;
    while(cin >> n)
    {
        if(n == 0)
            break;
        cnt++;
        for(int i = 1; i <= n; ++i)
            cin >> a[i].x >> a[i].y;
        for(int i = 1; i <= n; ++i)
            for(int j = i + 1; j <= n; ++j)
                dis[i][j] = dis[j][i] = sqrt((a[i].x - a[j].x) * (a[i].x - a[j].x) + (a[i].y - a[j].y) * (a[i].y - a[j].y));
        dijkstra(1);
        printf("Scenario #%d\nFrog Distance = %.3f\n\n", cnt, ans[2]);
    }
    return 0;
}

Code of Floyd

#include <cmath>
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
const int inf = 0x3f3f3f3f;

int n;
struct node
{
    double x, y;
}a[205];

double dis[505][505];///dis[i][j]存从i到j所有路的所有路段中最长的长度

void floyd()
{
    for(int k = 1; k <= n; ++k)
        for(int i = 1; i <= n; ++i)
            for(int j = 1; j <= n; ++j)
                dis[i][j] = min(dis[i][j], max(dis[i][k], dis[k][j]));
}

int main()
{
    int cnt = 0;
    while(cin >> n)
    {
        if(n == 0)
            break;
        cnt++;
        for(int i = 1; i <= n; ++i)
            for(int j = 1; j <= n; ++j)
            dis[i][j] = (i == j ? 0 : inf);
        for(int i = 1; i <= n; ++i)
                cin >> a[i].x >> a[i].y;
        for(int i = 1; i <= n; ++i)
            for(int j = i + 1; j <= n; ++j)
            dis[i][j] = dis[j][i] = sqrt((a[i].x - a[j].x) * (a[i].x - a[j].x) + (a[i].y - a[j].y) * (a[i].y - a[j].y));
        floyd();
        printf("Scenario #%d\nFrog Distance = %.3f\n\n", cnt, dis[1][2]);
    }
    return 0;
}
Site by Baole Zhao | Powered by Hexo | theme PreciousJoy