POJ - 2253 - Frogger (Dijkstra)

来源:互联网 发布:win10 笔记软件 编辑:程序博客网 时间:2024/04/28 23:43

Frogger
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 27362 Accepted: 8895

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

20 03 4317 419 418 50

Sample Output

Scenario #1Frog Distance = 5.000Scenario #2Frog Distance = 1.414

Source

Ulm Local 1997





思路:这题题意不好理解,题目所求是第一个节点到第二个节点的“最短路”中多条边中的最大边,这里的最短路为多条边中的最短边,,,,因为当他到每个点的距离都为"最短路"时,,才会产生到第二个节点的“最短路”,,才会有“最短路”中多条边中的最大边(也可以理解为尽可能的压缩那个路径(第一个节点到第二个节点)中的每一条边,使得每一条边尽可能小(“最短路”),这样才会得到最小的最大边,最短路可实现。)


AC代码:

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <cmath>#define INF 0x7f7f7f7fusing namespace std;const int maxn = 205;double map[maxn][maxn];double dis[maxn], vis[maxn];//dis[i]表示当前已知的从源点到顶点i的"最短路径"的长度  int n;struct Point {double x, y;}p[maxn];double getlength(Point a, Point b) {return sqrt( (b.y - a.y)*(b.y - a.y) + (b.x - a.x)*(b.x - a.x) );}double dijk() {for(int i = 1; i <= n; i++) dis[i] = map[1][i];dis[1] = 0; vis[1] = 1;double max = 0;int pos = 1;while(pos != 2) {double min = INF;for(int j = 1; j <= n; j++) if(!vis[j] && dis[j] < min)min = dis[pos = j];if(min > max) max = min;vis[pos] = 1;for(int j = 1; j <= n; j++) if(!vis[j] && map[pos][j] < INF &&map[pos][j] < dis[j])dis[j] = map[pos][j];}return max;}int main() {int cas = 1;while(scanf("%d", &n) == 1 && n) {memset(vis, 0, sizeof(vis));for(int i = 1; i <= n; i++) {scanf("%lf %lf", &p[i].x, &p[i].y);}for(int i = 1; i <= n; i++) {for(int j = 1; j <= n; j++) {map[i][j] = getlength(p[i], p[j]);}}double ans = dijk();printf("Scenario #%d\nFrog Distance = %.3lf\n\n", cas++, ans);}return 0;} 












1 0
原创粉丝点击