POJ 2253 - Frogger(最短路`dijkstra)

来源:互联网 发布:淘宝图片签署协议在哪 编辑:程序博客网 时间:2024/05/01 15:32

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

20 03 4317 419 418 50

Sample Output

Scenario #1Frog Distance = 5.000Scenario #2Frog Distance = 1.414

                                                        

题意:

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

思路:

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

CODE:

#include <iostream>#include <cstdio>#include <cmath>#include <cstring>#include <vector>#include <queue>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<double, int> P;int N;vector<edge> 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<P, vector<P>, greater<P> > 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;}


0 0
原创粉丝点击