poj2253 Frogger(Floyd)

题目链接

http://poj.org/problem?id=2253

题意

给出青蛙A,B和若干石头的坐标,现在青蛙A要跳到青蛙B所在的石头上,求出所有路径中最远那一跳的最小值。

思路

Floyd算法的变形,将求两点之间的最短路改成求两点之间最大边权的最小值即可。

代码

 1 #include <algorithm>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <cstdio>
 5 #include <cmath>
 6 using namespace std;
 7 
 8 const int N = 210;
 9 int x[N];
10 int y[N];
11 double dist[N][N];
12 int n;
13 
14 double get_dist(int i, int j)
15 {
16     return sqrt((x[i]-x[j])*(x[i]-x[j]) + (y[i]-y[j])*(y[i]-y[j]));
17 }
18 
19 void floyd()
20 {
21     for(int k=0; k<n; k++)
22         for(int i=0;i<n; i++)
23             for(int j=0; j<n; j++)
24                 dist[i][j] = min(dist[i][j], max(dist[i][k], dist[k][j]));  
25 }
26 
27 int main()
28 {
29     //freopen("poj2253.txt", "r", stdin);
30     int kase = 1;
31     while(cin>>n && n)
32     {
33         memset(x, 0, sizeof(x));
34         memset(y, 0, sizeof(y));
35 
36         for(int i=0; i<n; i++)
37             cin>>x[i]>>y[i];
38 
39         for(int i=0; i<n; i++)
40             for(int j=0; j<n; j++)
41                 dist[i][j] = get_dist(i, j);
42 
43         floyd();
44 
45         printf("Scenario #%d
", kase++);
46         printf("Frog Distance = %.3f

", dist[0][1]);
47     }
48     return 0;
49 }

注意点

1、由于距离是double类型,输出的时候是可以用%.3lf的,但这题使用%.3lf会WA,要使用%.3f 来输出。

原文地址:https://www.cnblogs.com/sench/p/7815336.html