EricYang

Tech Spot of Eric

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
Frogger
Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 11350
Accepted: 3805

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


解题思路:prim求最小生成树

               算出每个点间的距离,生成邻接矩阵

               求出最小生成树中最长的一段距离

               一旦目的节点加入最小生成树,结束算法

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>

#define MAX_VALUE 1e8

using namespace std;

typedef struct
{
    int x;
    int y;
}point;

int main()
{
    int times=0;
    int n;
    bool visit[201];
    int d[201];
    point stone[200];
    int map[201][201];
    int frogDist;

    while(scanf("%d",&n) && n!=0)
    {
        //init
        times++;
        memset(visit,0,sizeof(visit));
        memset(stone,0,sizeof(stone));
        for(int i=1; i<=n; i++)
        {
            d[i]=MAX_VALUE;
        }
        for(int i=1; i<=n; i++)
        {
            scanf("%d%d",&stone[i].x, &stone[i].y);
        }
        for(int i=1; i<=n; i++)
        {
            for(int j=i+1; j<=n; j++)
            {
                    map[i][j]=(stone[i].x-stone[j].x)*(stone[i].x-stone[j].x)+(stone[i].y-stone[j].y)*(stone[i].y-stone[j].y); //dist is square of real dist
                    map[j][i]=map[i][j];
            }
            map[i][i]=MAX_VALUE;
        }
        frogDist=0;
        //prim
        for(int i=1; i<=n; i++)
        {
            //Extract min_value node
            d[1]=0;
            int minNode=1;
            int minValue=MAX_VALUE;
            for(int j=1; j<=n; j++)
            {
                if(!visit[j] && d[j]<minValue)
                {
                    minNode=j;
                    minValue=d[j];
                }
            }
            if(frogDist<d[minNode])
                frogDist=d[minNode];
            visit[minNode]=true;
            if(minNode==2)
                break;
            //Finded minNode, and relax in adjcent node
            for(int j=1; j<=n; j++)
            {
                if(!visit[j]&&map[minNode][j]<d[j])
                {
                     d[j]=map[minNode][j];
                }
            }
        }
        printf("Scenario #%d\n",times);
        printf("Frog Distance = %.3f\n",sqrt(double(frogDist)));
        printf("\n");

    }

    return 0;
}

 

 

附加测试用例:

8
Scenario #1
1 1
4 0
1 2
2 2
3 2
4 2
3 0
5 1
Frog Distance = 1.414
3
Scenario #2
9 10
10 10
100 10
Frog Distance = 1.000

posted on 2010-12-31 17:38  Eric-Yang  阅读(453)  评论(0编辑  收藏  举报