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
解答:

 #include <iostream>//蓝牙博客个人注释版
 #include <cmath>
 #define MAX 201
 #define INF 527527.0
 using namespace std;
 float matrix[MAX][MAX];
 float dis[MAX];
bool used[MAX];

  struct node
 {
    int x,y;      
 };
 void DIJ(int &m)
 {
     for(int i=1;i<=m;++i)
     dis[i]=matrix[1][i];//用边长的权值作为初始值,等待后面的更新
     //注意这时matrix[1][1]等于INF,也即开始的时候第一个结点到第一个结点的距离初始化为无穷大了
     used[1]=true;//第一个顶点的值确定后就标记为真
     int tmp;
     for(int j=1;j<=m-1;++j)//还有m-1个顶点没确定
     {
          float minDis=INF;
          for(int k=1;k<=m;++k)
          {
            if(!used[k] && dis[k]<minDis)//所以第一次for 循环时没有往下走
            {minDis=dis[k]; tmp=k;}       
          } 
          used[tmp]=true;
          //上面的for循环完成后,就确定了从第一个顶点出发到其他顶点中距离最短的路径
          //然后将这个结点标记为真,下面的语句执行完后外层for的j加1(因为又多确定了一个),
         
          for(int k=1;k<=m;++k)//判断是否需要更新路径
          {
             if(!used[k] && matrix[tmp][k]!=INF)
             {
               dis[k]=min(dis[k],max(dis[tmp],matrix[tmp][k]));           
             }       
          }  
     }   
 }
 int main()
 {
     int m;
     int count=0;
     node p[MAX];
     while(cin>>m,m)
     {
        memset(matrix,INF,sizeof(matrix));
        memset(used,false,sizeof(used));
        for(int i=1;i<=m;++i)
        scanf("%d%d", &p[i].x, &p[i].y);
        //cin>>&p[i].x>>&p[i].y;
       // cin>>p[i].x>>p[i].y;
       
        for(int i=1;i<=m;++i)//构造图,用边长作为权值
           for(int j=i+1;j<=m;++j)//1-2 1-3 1-4等等
               matrix[i][j]=matrix[j][i]=sqrt((float)((p[i].x-p[j].x)*(p[i].x-p[j].x)+(p[i].y-p[j].y)*(p[i].y-p[j].y)));             
        
         DIJ(m);
         printf("Scenario #%d\nFrog Distance = %.3f\n\n",++count,dis[2]);
              
     }
     system("pause");
     return 0;
 }

posted on 2010-05-02 22:35  蓝牙  阅读(528)  评论(0)    收藏  举报