Frogger
Description
Freddy Frog is sitting on a stone inthe middle of a lake. Suddenly he notices Fiona Frog who is sittingon another stone. He plans to visit her, but since the water isdirty and full of tourists' sunscreen,
he wants to avoid swimmingand instead reach her by jumping.
Unfortunately Fiona's stone is out of his jump range. ThereforeFreddy considers to use other stones as intermediate stops andreach her by a sequence of several smalljumps.
To execute a given sequence of jumps, a frog's jump range obviouslymust be at least as long as the longest jump occuring in thesequence.
The frog distance (humans also call it minimax distance) betweentwo stones therefore is defined as the minimum necessary jump rangeover all possible paths between the twostones.
You are given the coordinates of Freddy's stone, Fiona's stone andall other stones in the lake. Your job is to compute the frogdistance between Freddy's and Fiona'sstone.
Input
The input will contain one or moretest cases. The first line of each test case will contain thenumber of stones n (2<=n<=200). Thenext 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, theother n-2 stones are unoccupied. There's a blank line followingeach test case. Input is terminated by a value of zero (0) forn.
Output
For each test case, print a linesaying "Scenario #x" and a line saying "Frog Distance = y" where xis replaced by the test case number (they are numbered from 1) andy is replaced by the appropriate real number, printed to threedecimals. Put a blank line after each test case, even after thelast 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
一直从最小的边加,当1与2在同一个集合中,当前边可以就是最小的。
另:也可以二分答案,二分这个最大值,把比这个值大的删去,看看是否能从1->2
#include<cmath>
#include<vector>
#include<cstdio>
#include<string>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define MAXN 1010
#define INF 100000007
using namespace std;
double g[MAXN][MAXN];
int x[MAXN],y[MAXN];
struct Edge{
int fm,to;
double dist;
}e[MAXN];
int f[MAXN];
int n,m;
void cal(){
for(int i = 1;i <= n; i++)
for(int j = i+1; j <= n;j++)
{
g[i][j]=sqrt((double)((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])));
g[j][i]=g[i][j];
}
}
void init(){
int xx,yy,w;
for (int i = 0; i <= n; i++)
for (int j = 0; j <= n; j++) g[i][j]=INF;
for (int i = 1; i <= n; i++)
scanf("%d%d",&x[i],&y[i]);
cal();
m = 0;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n;j++) if (i!=j)
{
e[++m].dist = g[i][j];
e[m].fm = i;
e[m].to = j;
}
}
int find(int x) {
if (f[x]!=x) f[x]=find(f[x]);
return f[x];
}
void merge(int x,int y){
int xx=find(x);
int yy=find(y);
f[xx]=yy;
}
bool cmp(Edge a,Edge b){
return a.dist<b.dist;
}
double kruskal(){
sort(e+1,e+m+1,cmp);
for(int i=1;i<=n;i++) f[i]=i;
int k = 0;
while (k++ < m-1)
{
int x=e[k].fm,y=e[k].to;
merge(x,y);
if (find(1)==find(2)) return e[k].dist;
}
}
int main(){
int iCase = 0;
while (scanf("%d",&n) && n)
{
init();
printf("Scenario #%d\n",++iCase);
printf("Frog Distance = %.3f\n\n",kruskal());
}
return 0;
}