E. Frogger

来源:互联网 发布:佛教 知乎 编辑:程序博客网 时间:2024/05/16 05:46

题目链接:http://www.bnuoj.com/v3/contest_show.php?cid=6511#problem/E

题目描述:

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.

输入格式:

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.

输出格式:

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.

输入样例:

20 03 4317 419 418 50
输出样例:

Scenario #1Frog Distance = 5.000Scenario #2Frog Distance = 1.414
思路分析:

本题定义的距离与以往有些不同,本题的距离是每条路径上单点到下一点的距离的最大值为其距离,典型的弗洛伊德算法,只是三层循环中的方程不一样,因为距离的定义格式不一样,先要求出有中转点路径下的“距离”,再与当前进行比较,最后d[0][1]就是要求的值,因为第一个点与第二个点分别为起点和终点。

源代码如下:

//Floyd algorithm#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>using namespace std;#define MAXN 200 + 10#define INF 0x3f3f3f3fstruct Node {    double x, y;};Node s[MAXN];int nn;double d[MAXN][MAXN];int main() {    //FILE *p = freopen("test.txt", "r", stdin);    int i, j, k;    int c = 1;    while (~scanf("%d", &nn) && nn) {        for (i = 0; i < nn; i++) {            scanf("%lf%lf", &s[i].x, &s[i].y);        }        //memset(d, INF, sizeof(d));        for (i = 0; i < nn; i++) {            for (j = 0; j < nn; j++) {                d[i][j] = (s[i].x-s[j].x)*(s[i].x-s[j].x)+(s[i].y-s[j].y)*(s[i].y-s[j].y);            }        }        for (k = 0; k < nn; k++) {            for (i = 0; i < nn; i++) {                for (j = 0; j < nn; j++) {                    d[i][j] = min(d[i][j], max(d[i][k], d[k][j]));                }            }        }        double ans = sqrt(d[0][1]);             //最后开方,保证精度        printf("Scenario #%d\nFrog Distance = %.3f\n\n", c++, ans);    }    return 0;}


0 0
原创粉丝点击