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.
题意:给n个点,求出第一个点到第二个点的多条路径中任意两点最长距离的最短的距离。
可以看做dijkstra的变形,贪心思想,用数组d[i]代表i到源点路径中相邻两点的最长距离。
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
#include<cmath>
#include<functional>
using namespace std;
#define N 1000+5
#define MAXN 1000000
#define mem(arr,a) memset(arr,a,sizeof(arr))
#define INF 0x3f3f3f3f
#define LL long long int
#define pow(a) (a)*(a)
double d[N];
struct node{
int x, y;
}p[N];
int vis[N];
double cost[N][N];
int n;
int cnt = 1;
double dis(node a, node b){
return sqrt((double)pow(a.x - b.x) + (double)pow(a.y - b.y));
}
void dijkstra(){
mem(d, 0);
mem(vis, 0);
while (1){
int v = -1;
for (int i = 1; i <= n; i++){
if (!vis[i] && (v == -1 || d[v] > d[i]))v = i;
}
if (v == -1)break;
vis[v] = 1;
for (int i = 1; i <= n; i++){
if (!vis[i])
{
if (!d[i])d[i] = max(d[v], cost[v][i]);
else if (d[i] < d[v])continue;
else d[i] = min(d[i], max(d[v], cost[v][i]));
}
}
}
printf("Scenario #%d\nFrog Distance = %.3f\n\n", cnt++, d[2]);
}
int main(){
while (cin >> n){
if (n == 0)break;
for (int i = 1; i <= n; i++){
cin >> p[i].x >> p[i].y;
}
mem(cost, 0);
for (int i = 1; i <= n; i++){
for (int j = 1; j <= n; j++){
cost[i][j] = dis(p[i], p[j]);
}
}
dijkstra();
}
}