POJ 2253 - Frogger(最短路`dijkstra)

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.
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

                                                        

题意:

从起点a到终点b,需利用几个点作为过渡点,从a到b,使得每一次跳跃的距离最小,求出这些跳跃值的最大值。

思路:

dijkstra算法,将其中求最短距离的数组d【】改为记录每次跳跃的最短距离。注意要在记录d[]的过程中一边更新ans值。

CODE:

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

using namespace std;
const double INF = 10000000.0;
const int MAXN = 205;
struct node { double x, y; } stone[MAXN];
struct edge { int to; double cost; };
typedef pair P;

int N;
vector g[250];
double d[MAXN], ans;

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

void dijkstra(int s)
{
    ans = 0.0;
    priority_queue, greater

> que; fill(d, d + N + 1, INF); d[s] = 0.0; que.push(P(0.0, s)); while(!que.empty()) { P p = que.top(); que.pop(); int v = p.second; if(d[v] > p.first) continue; if(d[v] != INF) ans = max(ans, d[v]); if(v == 2) return; for(int i = 0; i < g[v].size(); ++i) { edge e = g[v][i]; if(d[e.to] > e.cost) { d[e.to] = e.cost; que.push(P(d[e.to], e.to)); } } } } int main() { //freopen("in", "r", stdin); int cas = 0; while(~scanf("%d", &N)) { if(N == 0) break; cas ++; for(int i = 0; i <= N; ++i) { g[i].clear(); } edge e; for(int i = 1; i <= N; ++i) { scanf("%lf %lf", &stone[i].x, &stone[i].y); for(int j = 1; j < i; ++j) { if(i == j) continue; e.cost = dis(stone[i], stone[j]); e.to = j; g[i].push_back(e); e.to = i; g[j].push_back(e); } } if(N == 2) { double ans = dis(stone[1], stone[2]); printf("Scenario #%d\nFrog Distance = %.3f\n\n", cas, ans); continue; } dijkstra(1); printf("Scenario #%d\nFrog Distance = %.3f\n\n", cas, ans); } return 0; }



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