poj 2253 Frogger(变式Dijkstra)

来源:互联网 发布:中标麒麟安装软件 编辑:程序博客网 时间:2024/05/29 08:16

poj 2253 Frogger(变式Dijkstra)
Time Limit: 1000ms Memory Limit: 65536kB

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

Source
Ulm Local 1997


这个题是最近复习最短路时候从别人归纳的题库里找出来的。做别人的题库有好处也有坏处,好处是针对性强,难度适中,不会遇到那种很少人做的不会做也搜不到的题解的生僻题目,坏处是我瞄了一眼就看到做法了——变式Dijkstra,一看题意(这个Frog Distance指的是不同道路上最大边长度的最小值,很有现实意义,你跳的比Frog Distance远就可以跳到否则跳不到),如果类比成传统权值距离(事实上,这和传统权值距离距差很多,比如传统权值距离abc路径长度=ab路径长度+bc路径长度,而这里abc路径长度=max{ab路径长度,bc路径长度},所以这个类比是有风险的),而且问题是单源的,一看就觉得要用变式Dijkstra写(只需修改更新min时候距离的定义即可),所以很快写下来了,一遍AC。
那么问题来了,Dijkstra变式依据是什么,怎样定义的路径总长度可以用Floyd,Dijkstra算法呢?这个问题比较严肃,也需要对这些经典最短路算法有很好的理解,也有助于理解这些算法,所以我觉得很有必要好好考虑,并且写一遍博文讨论这个问题。


Accepted    256kB   1ms 1024 B  G++ 
#define MAX_N 200#include<stdio.h>#include<memory.h>#include<math.h>int scenario=0,n;int pos[MAX_N][2],dis2[MAX_N][MAX_N],min[MAX_N],visit[MAX_N],i0;inline int Sqr(int x){    return x*x;}inline int Min(int x,int y){    return x<y?x:y;}inline int Max(int x,int y){    return x>y?x:y;}int main(){    while ((scanf("%d",&n)==1)&&(n!=0))    {        scenario++;        if (scenario>1)            printf("\n");        memset(visit,false,sizeof(visit));        for (int i=0;i<n;i++)            scanf("%d%d",&pos[i][0],&pos[i][1]);        for (int i=0;i<n;i++)            for (int j=0;j<n;j++)                dis2[i][j]=Sqr(pos[i][0]-pos[j][0])+Sqr(pos[i][1]-pos[j][1]);        for (int i=0;i<n;i++)            min[i]=dis2[0][i];        visit[0]=true;        while (!visit[1])        {            i0=-1;            for (int i=0;i<n;i++)                if (!visit[i])                {                    if (i0==-1)                        i0=i;                    else if (min[i]<min[i0])                        i0=i;                }            visit[i0]=true;            for (int i=0;i<n;i++)                min[i]=Min(min[i],Max(min[i0],dis2[i0][i]));        }        printf("Scenario #%d\nFrog Distance = %.3f\n",scenario,sqrt(min[1]));    }    return 0;}
0 0