zoukankan      html  css  js  c++  java
  • Frogger dijstra算法

    Problem 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
    ***************************************************************************************************************************
    dij
    **************************************************************************************************************************
     1 /*
     2 dijstra算法,更新到2时输出结果
     3 */
     4 #include<iostream>
     5 #include<string>
     6 #include<cstring>
     7 #include<cmath>
     8 #include<cstdio>
     9 #include<queue>
    10 #define  inf  99999999
    11 using namespace std;
    12 double dis[10001];
    13 double map[1001][1001];
    14 int n,i,j,k;
    15 double x[1001],y[1001];
    16 int vis[1001],num;
    17 double  max(double a,double b)
    18  {
    19      if(a>b) return a;
    20      return b;
    21  }
    22 double dist(double x1,double y1,double x2,double y2)
    23 {
    24     return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
    25 }
    26 
    27 void  dijstra()
    28 {
    29     memset(vis,0,sizeof(vis));
    30     for(int it=1;it<=n;it++)
    31      {
    32          dis[it]=map[1][it];
    33          //printf("dis[%d]:%lf
    ",it,dis[it]);
    34      }
    35      vis[1]=1;
    36      int k;
    37      double min1;
    38     for(int it=1;it<n;it++)
    39     {
    40         min1=inf;
    41         for(int jt=1;jt<=n;jt++)
    42         {
    43             if(min1>dis[jt]&&!vis[jt])
    44             {
    45                 min1=dis[jt];
    46                 //cout<<"dis::"<<dis[jt]<<endl;
    47                 k=jt;
    48             }
    49         }
    50          //cout<<"dis::"<<dis[it]<<endl;
    51         //cout<<"k:  "<<k<<endl;
    52         vis[k]=1;
    53         if(k==2)
    54        {
    55         cout<<"Scenario #"<<num<<endl;
    56         cout<<"Frog Distance = ";
    57         printf("%.3f
    
    ",dis[2]);
    58         break;
    59        }
    60 
    61         for(int jt=1;jt<=n;jt++)
    62         {
    63             if(dis[jt]>max(dis[k],map[k][jt])&&!vis[jt])
    64                 dis[jt]=max(dis[k],map[k][jt]);
    65         }
    66     }
    67 
    68 }
    69 int main()
    70 {
    71     num=0;
    72 
    73     while(scanf("%d",&n)&&n)
    74     {
    75         memset(dis,0,sizeof(dis));
    76         for(i=1;i<=n;i++)
    77          for(j=1;j<=n;j++)
    78           map[i][j]=inf;
    79         for(i=1;i<=n;i++)
    80         {
    81             scanf("%lf%lf",&x[i],&y[i]);
    82         }
    83         for(i=1;i<=n;i++)
    84          for(j=1;j<=n;j++)
    85          {
    86             map[i][j]=dist(x[i],y[i],x[j],y[j]);
    87             map[j][i]=map[i][j];
    88             //printf("map[%d][%d]: %lf
    ",i,j,map[i][j]);
    89          }
    90          ++num;
    91          dijstra();
    92     }
    93     return 0;
    94 }
    View Code

  • 相关阅读:
    JavaScript 继承机制设计思想
    JavaScript里的原型(prototype), 原型链,constructor属性,继承
    centos7用yum安装node.js v8.x
    sequelize 字段无法操作
    开发CLI命令行
    微信分组群发图文40152,微信分组群发图文invalid group id hint
    微信分组群发45028,微信分组群发has no masssend quota hint
    微信45028错误,微信has no masssend quota hint错误
    微信上传图文消息素材40007,invalid media_id hint
    Spring文件上传出错:java.lang.ClassCastException: org.apache.catalina.connector.Request
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3378265.html
Copyright © 2011-2022 走看看