Frogger
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
题目类型:最大容量路(Floyd思想)
算法分析:将读入的坐标转换为邻接阵中的权值,然后使用Floyd算法迭代n - 1次插点计算最大容量,其中更新方程为:edge[i][j] = min (edge[i][j], max (edge[i][k], edge[k][j]))
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
#include <iostream> #include <fstream> #include <algorithm> #include <iomanip> #include <cstring> #include <cstdio> #include <cmath> #include <map> #include <string> #include <vector> #include <stack> #include <queue> #include <set> #include <list> #include <ctime> using namespace std; const int maxn = 666 + 66; const int INF = 0x7fffffff; const double EPS = 1e-6; struct Node { int x, y; }; Node ans[maxn]; double edge[maxn][maxn]; int n; void floyd () { int i, j, k; for (k = 0; k < n; k++) { for (i = 0; i < n; i++) { for (j = 0; j < n; j++) edge[i][j] = min (edge[i][j], max (edge[i][k], edge[k][j])); } } } int main() { // ifstream cin ("aaa.txt"); int flag = 1; while (cin >> n) { if (n == 0) break; int i; for (i = 0; i < n; i++) cin >> ans[i].x >> ans[i].y; int j; for (i = 0; i < n; i++) for (j = 0; j < n; j++) { if (i == j) edge[i][j] = 0; else edge[i][j] = INF; } for (i = 0; i < n - 1; i++) { for (j = i + 1; j < n; j++) { double temp = sqrt (pow (ans[i].x - ans[j].x, 2) + pow (ans[i].y - ans[j].y, 2)); edge[i][j] = temp; edge[j][i] = temp; } } floyd (); cout << "Scenario #" << flag++ << endl; cout << "Frog Distance = " << fixed << setprecision (3) << edge[0][1] << endl << endl; } return 0; } |
- « 上一篇:zoj1718
- zoj1948:下一篇 »