Frogger POJ - 2253
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.
Unfortunately Fiona’s stone is out of his jump range. Therefore Freddy considers to use other stones as intermediate stops and reach her by a sequence of several small jumps.
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 defined as the minimum necessary jump range over all possible paths between the two stones.
You are given the coordinates of Freddy’s stone, Fiona’s stone and all other stones in the lake. Your job is to compute the frog distance between Freddy’s and Fiona’s stone.
Input
The input will contain one or more test cases. The first line of each test case will contain the number of stones n (2<=n<=200). The next 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, the other n-2 stones are unoccupied. There’s a blank line following each test case. Input is terminated by a value of zero (0) for n.
Output
For each test case, print a line saying “Scenario #x” and a line saying “Frog Distance = y” where x is replaced by the test case number (they are numbered from 1) and y is replaced by the appropriate real number, printed to three decimals. Put a blank line after each test case, even after the last 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到n之间有多条路径,每条路径都有一个最大距离,最大距离是指组成这条路径的路的最大值。求所有路径中最大距离的最小值。
思路:从终点1出发,找距离1最短距离的路口为2,记录,然后找距离2最短的路口,直至找到起始点0。此时找到的这条路就是拥有最小最大距离的路径。而这条路径的最大值就是所求的最小必要跳跃距离。其实,这是个最小生成树问题。
这题刚开始做时没一点思路,后来AK大佬来看了看,当即想思路,我敲代码,然后就A了。。不得不说,真强。(感谢)。以后的路还很长。
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int inf=1000000;
const int maxn=205;
bool vis[maxn];
int n;
int num=1;
double map[maxn][maxn];//每个石头之间的距离。
double d[maxn];//终点到各个石头之间的距离
double lz[maxn];//记录含有最小必要跳跃距离的路径
struct node{
double x,y;//石头坐标
}a[1000];
void dd()
{
int xb=0;
memset(vis,0,sizeof(vis));
memset(d,0,sizeof(d));
memset(lz,0,sizeof(lz));
fill(d,d+maxn,inf);
d[1]=0;//终点到自身的距离为0
for(int i=0;!vis[0];i++)
{
double min=inf;
int u=-1;
for(int j=0;j<n;j++)//找出距现有生成树最小的点
{
if(!vis[j]&&d[j]<min)
{
min=d[j];
u=j;
}
}
vis[u]=true;
lz[xb++]=min;//记录路径,及距离
for(int v=0;v<n;v++)
{
if(!vis[v]&&d[v]>map[u][v])//更新
{
d[v]=map[u][v];
}
}
}
// for(int i=0;i<n;i++) 这部分是为了debug
// cout<<i<<":"<<vis[i]<<" "<<d[i]<<endl;
// for(int i=0;i<xb;i++)
// cout<<lz[i]<<" ";
// cout<<endl;
sort(lz,lz+xb);//排序,找最小必要跳跃距离
cout<<"Scenario #"<<num++<<endl;
printf("Frog Distance = %.3lf\n\n",lz[xb-1]);
}
double jz(node x,node y)
{
double dis=0;
dis=sqrt(pow((y.x-x.x),2)+pow(y.y-x.y,2));
return dis;
}
int main()
{
while(cin>>n,n)
{
memset(map,0,sizeof(map));
int i,j=0;
for(i=0;i<n;i++)
{
cin>>a[i].x>>a[i].y;
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
map[i][j]=jz(a[i],a[j]);//求各点之间的距离
}
}
dd();
}
return 0;
}