B

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.

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

初看似乎很难把这道题与单源最短路联系在一起。但我们可以做以下分析:

Dijkstra的贪心思想是基于这样一种前提:

设A→P1→P2→P3→B是A到B的最短路,则其中任意一部分亦是最短路。例如P1→P2→P3必然是P1到P3的最短路。证明很简单。假设P1到P3存在一条更短的路,则A到B便可通过该路获得一条更短的路,与题设“A→P1→P2→P3→B是A到B的最短路”矛盾。

由此便得到了Dijkstra的松弛方程:

枚举u。if (d[v]>d[u]+w(u,v)) then d[v]←d[u]+w(u,v)

其中d为源到点最短路长度。u为中转节点。w为u到v的最短路长度。

这是否能和本题的求解过程等同呢?

对于本题,易得两点间最长边最短的路径可能不唯一。但题目只关心点1到点2所有路径中最短的最长边的长度,并不关心具体路径。所以我们在求解过程中,可以人为定义所求路径满足以下条件:

设A→P1→P2→P3→B是我们想要求得的路径,则它满足其中任意一部分亦满足最长边最短。例如P1→P2→P3亦是所有P1到P3路径中最长边最短的。

这样一来,本题的求解,也满足了与Dijkstra相似的贪心过程。它的松弛方程为:

枚举j。if(dis[j]>max(dis[k],map[k][j])) then dis[j]=max(dis[k],map[k][j])

它的意思是,枚举所有中间点k,源到j的目标路径的最长边,要么在源到k这段,要么在k到j这段。

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <deque>
//#include <unordered_set>
//#include <unordered_map>
//#include <xfunctional>
#define ll  long long
#define PII  pair<int, int>
using namespace std;
int dir[5][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 } ,{ 0,0 } };
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int inf = 0x3f3f3f3f;
const double pi = 3.14159265358979;
const int mod = 1e9 + 7;
const int maxn = 205;
//if(x<0 || x>=r || y<0 || y>=c)
//1000000000000000000
double x[maxn], y[maxn];
int sizedis = maxn*maxn;
double v[maxn][maxn];
vector<double> dis(sizedis, inf);
double dijkstra(int n)
{
    for (int i = 1; i <= n; i++)
        dis[i] = v[i][1];
    dis[1] = 0;
    vector<bool> vis(maxn, false);
    for (int i = 1; i < n; i++)
    {
        int mini = 0;
        double minn = inf;
        for (int j = 1; j <= n; j++)
        {
            if (!vis[j] && dis[j] < minn)
            {
                mini = j;
                minn = dis[j];
            }
        }
        vis[mini] = true;
        for (int j = 1; j <= n; j++)
        {
            if (!vis[j])
            {
                double mmax = max(minn, v[j][mini]);
                if (mmax < dis[j])
                    dis[j] = mmax;
            }
        }
    }
    return dis[2];
}
int main()
{
    int n,scen=0;
    while (cin >> n && n != 0)
    {
        scen++;
        cout << "Scenario #" << scen << endl;
        for (int i = 1; i <= n; i++)
        {
            cin >> x[i];
            cin >> y[i];
        }
        for (int i = 1; i <= n; i++)
        {
            for (int j = i + 1; j <= n; j++)
                v[i][j] = v[j][i] = sqrt((x[i] - x[j])*(x[i] - x[j]) + (y[i] - y[j])*(y[i] - y[j]));
        }
        cout << "Frog Distance = ";
        printf("%.3f

", dijkstra(n));
    }
    return 0;
}
原文地址:https://www.cnblogs.com/dealer/p/12602347.html