POJ 2253 Frogger(SPFA运用)

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的所有路径上最长边的最小值。
思路:对于给定的坐标我们可以将之转换成一个距离矩阵,距离由两点间距离公式求出来,之后用一遍最短路就OK啦(不过在松弛的时候是求最长边的最小值)

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<iomanip>
 5 #include<vector>
 6 #include<queue>
 7 #include<cmath>
 8 
 9 using namespace std;
10 const int INF = 900000000;
11 struct point {
12     int x, y;
13 }e[220];
14 int n, vis[220], f[220];
15 double mp[220][220], dis[220];
16 void SPFA(int s)
17 {
18     for (int i = 1; i <= n; i++) {
19         dis[i] = INF;
20         vis[i] = 0; f[i] = 0;
21     }
22     queue<int>Q;
23     dis[s] = 0; vis[s] = 1; f[s]++;
24     Q.push(s);
25     while (!Q.empty()) {
26         int t = Q.front(); Q.pop();
27         vis[t] = 0;
28         for (int i = 1; i <= n; i++) {
29             if (dis[i] > max(dis[t], mp[t][i])) {
30                 dis[i] = max(dis[t], mp[t][i]);
31                 if (!vis[i]) {
32                     vis[i] = 1;
33                     Q.push(i);
34                     if (++f[i] > n)return;
35                 }
36             }
37         }
38     }
39 }
40 int main()
41 {
42     ios::sync_with_stdio(false);
43     int T = 1;
44     while ((cin >> n)) {
45         if (n == 0)break;
46         for (int i = 1; i <= n; i++)cin >> e[i].x >> e[i].y;
47         if (n == 2) {
48             double dis = sqrt((e[1].x - e[2].x)*(e[1].x - e[2].x) + (e[1].y - e[2].y)*(e[1].y - e[2].y));
49             cout << "Scenario #" << T++ << endl;
50             cout << "Frog Distance = " << fixed << setprecision(3) << dis << endl << endl;
51             continue;
52         }
53         for (int i = 1; i <= n; i++)
54             for (int j = 1; j <= i; j++)//求出ij之间的距离转换成距离矩阵
55                 mp[i][j] = mp[j][i] = sqrt((double)((e[i].x - e[j].x)*(e[i].x - e[j].x)) + (double)((e[i].y - e[j].y)*(e[i].y - e[j].y)));
56         SPFA(1);
57         cout << "Scenario #" << T++ << endl;
58         cout << "Frog Distance = " << fixed << setprecision(3) << dis[2] << endl << endl;//行末两个换行!!!!!
59     }
60 
61     return 0;
62 }

猜你喜欢

转载自www.cnblogs.com/wangrunhu/p/9496702.html