题目关键语句:
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 definedas the minimum necessary jump range over all possible paths
between the two stones.
这句话我读了好多遍,才读懂题目是怎么定义最小距离,原来是每一条可达路径中都有一条最大边,问每一种可达情况下的最大边的最小值。起点 A 是节点 1,终点 B 是节点 2。
数据范围:
节点数量 2 < = n < = 200 2 <= n <=200 2<=n<=200
思路:
这题用 Floyd, Dijkstra,SPFA最短路算法都可以,看了网上的博客,还可用Prim做。这里数据量比较小我选用Floyd ,
O
(
n
3
)
O(n^3)
O(n3)还可以接受。改下原本最短路的定义,任意两点的最短路可通过第三个中间点 k 松弛,d[i][j] = min(d[i][j], d[i][k]+d[k][j])
改为两点之间通路的最大边的最小值这个状态。则d[i][j] = min(d[i][j], max(d[i][k],d[k][j])
输入样例:
2
0 0
3 4
3
17 4
19 4
18 5
0
输出样例:
Scenario #1
Frog Distance = 5.000
Scenario #2
Frog Distance = 1.414
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int N=210;
const int inf=0x3f3f3f3f;
int n;
int x[N],y[N];
double d[N][N];
double dist(int x1,int y1,int x2,int y2)
{
return sqrt(double(x1-x2)*(x1-x2)+double(y1-y2)*(y1-y2));//在里边 double 防止精度不准
}
void floyd()
{
for(int k=1;k<=n;k++)
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
d[i][j]=min(d[i][j], max(d[i][k],d[k][j])); //i-->j的最大边的最小值,可以由 i-->k 和 k-->j 的最大边的最小值转移过来
}
int main()
{
int k = 0;
while(cin>>n)
{
if(n==0)
break;
for(int i=1;i<=n;i++)
scanf("%d %d",&x[i],&y[i]);
for(int i=1;i<=n;i++)
for(int j=i+1;j<=n;j++)
d[i][j]=d[j][i]=dist(x[i],y[i],x[j],y[j]);
floyd();
printf("Scenario #%d\nFrog Distance = %.3lf\n\n",++k,d[1][2]);
}
return 0;
}