Description
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
Output
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;
 }
 
                    
                     
                    
                 
                    
                 
         
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号