POJ

来源:互联网 发布:淘宝宝贝收藏链接 编辑:程序博客网 时间:2024/06/15 03:28
Frogger
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 49855 Accepted: 15844

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

题意:

一个青蛙要从一个石头跳到另一个石头上。青蛙有许多路可以选择,但是青蛙想每次跳跃的距离尽量小,所以要使路径中最长边的距离尽量小,找到这个值。即求路径最长边的最小值。

思路:

可以用最短路来做。dijkstra为例。给每个点编号。从0到1,dis存0点到当前点的最大边的最小值。用最短路的思路每次扩展一个点,相当于增加两条边,判断这两条边的最大值是否小于dis[i];如果小于dis[i],那么将dis[i]的值更新为这两条边的最大值;否则舍弃这两条边。实际上,当dis中可以扩展的点的最小值为dis[1]时,就可以退出循环了,因为不会有更小的边来更新dis[1]了。

可能有人会问题目要求整条路径上的最长边的最小值,但是你每次只判断两条边。因为路径是由这些边扩展而来的,所以你不断对扩展的两条边取最大值更新dis,到最后就把整条路径都包含了。

本题还可以用最小生成树来做。

代码:

#include<stdio.h>#include<string.h>#include<algorithm>#include<math.h>#include<vector>using namespace std;#define inf 1<<29int n;double mp[205][205],x[205],y[205],dis[205];bool vis[205];double dijkstra(){    memset(vis,0,sizeof vis);    for(int i=0;i<n;i++)        dis[i]=mp[0][i];    vis[0]=1;    for(int k=1;k<n;k++)    {        int tmp,minn=inf;        for(int i=1;i<n;i++)        {            if(!vis[i] && minn>dis[i])                minn=dis[tmp=i];        }        vis[tmp]=1;        if(tmp==1) break;        for(int i=0;i<n;i++)        {            if(!vis[i] && dis[i]>max(dis[tmp],mp[tmp][i]))                dis[i]=max(dis[tmp],mp[tmp][i]);        }    }    return dis[1];}int main(){    int cas=1;    while(scanf("%d",&n) && n)    {        for(int i=0;i<n;i++)            scanf("%lf%lf",&x[i],&y[i]);        for(int i=0;i<n;i++)        {            for(int j=0;j<n;j++)            {                if(i==j)                    mp[i][j]=0;                else                    mp[i][j]=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));            }        }        printf("Scenario #%d\n",cas++);        printf("Frog Distance = %.3f\n\n",dijkstra());    }    return 0;}





原创粉丝点击