POJ2253 Frogger

来源:互联网 发布:java 访问mongodb 编辑:程序博客网 时间:2024/05/30 05:21

青蛙先生

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

题意:

n个石头,给出这n个石头的坐标,现在欲从第1个石头到达第二个石头,跳跃距离(到达目标点时,跳过的最长边的长度),现在要求最小的跳跃距离。

解题思路:

这个题明显属于最短路径之类的问题,但是又有了一些不同。因为求的并不是最短路径。因此,它记录的di以及更新条件都有些不同。另外题目中给的是点。而不是边,因此要进行预处理。于是可以用dijisktra算法,再加上预处理。

附上代码:

#include <iostream>#include<cmath>#include<cstdio>#include<cstring>#include<cstdlib>using namespace std;#define MAX 205#define inf 123456789double map[MAX][MAX];//记录两点之间的距离int n;struct Node{int x,y;};Node A[MAX];//录入点的坐标bool vis[MAX];double d[MAX];//d这里记录的不是最短路径长度,而是最大边的值 //在走到i位置时,最大边的值。double dist(Node a, Node b){          //a,b两点之间的距离    return sqrt((double)((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y)));//计算距离的函数    //注意一定要把sqrt()函数的参数强转成double类型,否则会CE}void dijikstra(){ memset(vis,0,sizeof(vis)); //vis用来标记该点有没有加进图里 int i,j;//循环变量 double Min;//设置最小值 for(i=1;i<=n;i++) {    d[i]=(i==1)?0:inf;//将所有的距离都设置为无穷大 } //在设置最小值的时候,初始化也要将最小值设置为无穷大for(i=1;i<=n;i++){    Min=inf;//初始化最小值    int v;    for(j=1;j<=n;j++)    {        if(!vis[j]&&Min>=d[j]){            v=j;            Min=d[j];        }    }    vis[v]=1;//该点已经加入了图中    //更新节点    for(j=1;j<=n;j++)//在已走的路径中寻找最长路径    {    d[j]=min(d[j],max(d[v],map[v][j]));    //求得不是最小路径的长度    }}printf("%.3f\n",d[2]);//输出}int main(){    int i,j,tep;    int T=0;    while(cin>>n,n)    {        for(i=1;i<=n;i++)        {            cin>>A[i].x>>A[i].y;        }        //初始化        for(i=1;i<=n;i++)            for(j=1;j<=n;j++)        {          map[i][j]=map[j][i]=dist(A[i],A[j]);        }//        if(T)cout<<endl;        cout<<"Scenario #"<<(++T)<<endl;        cout<<"Frog Distance = ";        dijikstra();    }    return 0;}
原创粉丝点击