题意: 有n个石头,各个石头之间都可以通过跳跃到达,试问从a[0]到a[1]的途中需要跳跃的最小最大距离为多少。答案保留三位小数。
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[1]到a[2]是绝对能到达的(至少可以直接a[0]跳到a[1]),所以我们只需Dijkstra()一遍就好。
- 用d[i]表示a[0]到a[i]过程中的最大跳跃距离,再用max(d[i],f[i][j])来更新d[j]就行。
代码实现:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <cmath>
#define null NULL
#define ll long long
#define pii pair<int, int>
#define lowbit(x) (x &(-x))
#define ls(x) x<<1
#define rs(x) (x<<1+1)
#define me(ar) memset(ar, 0, sizeof ar)
#define mem(ar,num) memset(ar, num, sizeof ar)
#define rp(i, n) for(int i = 0, i < n; i ++)
#define rep(i, a, n) for(int i = a; i <= n; i ++)
#define pre(i, n, a) for(int i = n; i >= a; i --)
#define IOS ios::sync_with_stdio(0); cin.tie(0);cout.tie(0);
const int way[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
using namespace std;
const double inf = 1e60;
const double PI = acos(-1.0);
const double eps = 1e-6;
const ll mod = 1e9 + 7;
const int N = 210;
int n, tt;
//f[i][j]表示i到j的直线距离
double d[N], f[N][N];
bool vis[N];
struct node{
int x, y;
}a[N];
//求出i和j两点的直线距离
double far(int i, int j) {
double x = a[i].x - a[j].x, y = a[i].y - a[j].y;
return (sqrt(pow(x, 2) + pow(y, 2)));
}
double Dijkstra()
{
me(vis);
//先初始化d[i]为a[0]到a[i]的直线距离
for(int i = 0; i < n; i ++)
d[i] = f[0][i];
d[0] = 0;
vis[0] = 1;
for(int i = 0; i < n - 1; i ++){
double t = inf, maxx; int x;
for(int j = 0; j < n; j ++)
if(!vis[j] && t >= d[j]) t = d[x = j];
vis[x] = 1;
for(int y = 0; y < n; y ++){
if(!vis[y]){
maxx = max(d[x], f[x][y]);
d[y] = min(d[y], maxx); //更新d[j]
}
}
}
return d[1];
}
int main()
{
while(~scanf("%d", &n)){
if(!n) return 0;
for(int i = 0; i < n; i ++)
scanf("%d%d", &a[i].x, &a[i].y);
for(int i = 0; i < n; i ++){
for(int j = 0; j < i; j ++)
f[i][j] = f[j][i] = far(i, j);
f[i][i] = 0;
}
printf("Scenario #%d\nFrog Distance = %.3f\n\n", ++ tt, Dijkstra());
}
return 0;
}