POJ—2253(Frogger)

xiaoxiao2021-02-28  48

Frogger Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 48830 Accepted: 15557

Description

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 C code

#include<stdio.h> #include<algorithm> #include<math.h> using namespace std; int main(void){ int a; int t=0; while(scanf("%d",&a)!=EOF){ if(a==0) break; int i,j; int leak[205][2],v[205];; double w[205][205],d[205]; double distance=0; if(a == 0 )break; for(i=0;i<a;i++) scanf("%d%d",&leak[i][0],&leak[i][1]); for(i=0;i<a;i++) { for(j=0;j<a;j++) //转化为两点之间距离,方便计算 if(i!=j) w[i][j] = 1000000; //信号量赋初值 v[i] = 0; d[i] = (i==0?0:1000000); } for(i=0;i<a;i++) for(j=0;j<a;j++) { int x1 = leak[i][0]; int x2 = leak[j][0]; int y1 = leak[i][1]; int y2 = leak[j][1]; //sqrt()函数需要强转否则会 CE distance=sqrt((double)((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2))); w[i][j] = distance; } /*此松弛操作解释: 首先,在 j = 0 时,通过此操作,可以求出0点到其余各点的距离,将0点标记 之后选取未标记最小边,将其一个节点j作为基准,依次连接所有点, 选择直接 0点到目标点 k 还是通过0到j点再到k点; 故松弛比较时,先取 0到j步骤和j到k步骤最大值,最后与 0直接到k 的值比较,取较小值;再将j点标记 重复此操作; */ for(i=0;i<a;i++) { int m = 1000000,x; //在未被选定的点中选取最小边值, //再以该点为基准,不断更新最值 for(j=0;j<a;j++) if(d[j]<=m && !v[j]){ x=j; m = d[j]; } v[x] = 1; for(j=0;j<a;j++) //松弛操作,选取最大值最小的路径 d[j] =min(d[j],max(w[x][j],d[x])); } printf("Scenario #%d\n",++t); //输出本题的解,即到相邻石头的值 printf("Frog Distance = %.3lf\n\n",d[1]); } }

转载请注明原文地址: https://www.6miu.com/read-59659.html

最新回复(0)