B - Frogger POJ - 2253(进阶最短路,青蛙跳跳跳)
作者:互联网
B - 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
题意:有一只青蛙要从A点跳到B点。青蛙由于身体机能限制,所以有一个极限跳跃远度。求:这个极限最少是多少,才能完成这个任务。点是二维坐标,其中第一个点是A点,第二个点是B点。
思路:先将石头的两两距离求出来存到a里,再对最短路的松弛操作变形一下就可以了。
注:有一个坑注意一下。Put a blank line after each test case, even after the last one.每个样例后输出一个空行。
代码:
#include <iostream>
#include <cstring>
#include <cmath>
#include <stdio.h>
#define inf 0x3f3f3f
using namespace std;
double a[202][202];
int via[202];
double dis[202];
int i,j;
double x[202];
double y[202];
int n;
double dijstra( int v0 )
{
for ( i=1; i<=n; i++ ) {
dis[i] = a[v0][i];
}
while ( 1 ) {
double minn = inf;
int u;
for ( i=1; i<=n; i++ ) {
if ( via[i]==0 && dis[i]<minn ) {
minn = dis[i];
u = i;
}
}
if ( minn==inf ) {
break;
}
via[u] = 1;
for ( i=1; i<=n; i++ ) {
if ( via[i]==0 && a[u][i]<inf && dis[i]>max(dis[u],a[u][i]) ) {
dis[i] = max(dis[u],a[u][i]); // 改变松弛条件
} // 最短路不再是路程的相加,
} //而是路程中两两的最大距离
}
return dis[2];
}
int main()
{
int listt=1;
while ( cin>>n && n!=0 ) {
memset(a,inf,sizeof(a));
memset(via,0,sizeof(via));
for ( i=1; i<=n; i++ ) {
cin >> x[i] >> y[i];
}
for ( i=1; i<=n; i++ ) { // 将所有两点的距离都求出来存起来
for ( j=1; j<=n; j++ ) {
a[i][j] = sqrt( pow(x[i]-x[j],2) + pow(y[i]-y[j],2) );
}
}
double ans = dijstra(1);
cout << "Scenario #" << listt << endl;
printf("Frog Distance = %.3f\n\n",ans); // 每组数据后加空行
listt ++;
}
return 0;
}
标签:stones,stone,202,进阶,int,POJ,test,line,2253 来源: https://blog.csdn.net/weixin_43828245/article/details/90723608