zoukankan      html  css  js  c++  java
  • POJ-2253-Frogger(最短路变形)

    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

    思路:青蛙距离就是,存在一条路径,这条路径的每一步的最大值,是所有路径里最小的。这个最大值就是所谓青蛙路径。我们吧Dijk算法中的判断两点中间路径,两段短还是一段短,换成两段的每段是不是都短于一段就ok。

    坑点:这个题有大坑!关于编译器的。

    1.由于G++保留了早期优化,所以数据类型会有变化。用double输入就要用%f输出而不能用%lf。

    具体如下

    C++也能用%f交,所以输出都用%f就好了

    以上内容转自(OJ提交题目中的语言选项里G++与C++的区别):https://blog.csdn.net/bat67/article/details/61926650

    2.G++中sqrt()可以开方int类型,而C++中sqrt()要转化为(double)类型。所以安全起见都转...

    3.注意输出格式...每个输出之间有一行间距...


     1 #include<cstdio>
     2 #include<cmath>
     3 #include<cstring>
     4 #include<algorithm>
     5 #define Inf 0x3f3f3f3f
     6 using namespace std;
     7 int n;
     8 double G[205][205],dis[205];
     9 int mark[205];
    10 
    11 struct node{
    12     int x,y;
    13 }p[205];
    14 
    15 double d(int x1,int y1,int x2,int y2){
    16     return sqrt((double)(x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
    17 }
    18 
    19 void Getmap(){
    20     for(int i=1;i<=n;i++){
    21         scanf("%d%d",&p[i].x,&p[i].y);
    22     } 
    23     memset(G,Inf,sizeof(G));
    24     for(int i=1;i<=n;i++){
    25         G[i][i]=0;
    26         for(int j=i+1;j<=n;j++){
    27             G[i][j]=G[j][i]=d(p[i].x,p[i].y,p[j].x,p[j].y);
    28         }        
    29     }    
    30 }
    31 
    32 void Dijk(){
    33     int mini,p=1;
    34     memset(mark,0,sizeof(mark));
    35     for(int i=1;i<=n;i++)
    36         dis[i]=G[1][i];    
    37     for(int k=0;k<=n;k++){
    38         mini=Inf;
    39         for(int i=1;i<=n;i++){
    40             if(!mark[i]&&dis[i]<mini){
    41                 mini=dis[i];
    42                 p=i;
    43             }
    44         }
    45         mark[p]=1;
    46         for(int i=1;i<=n;i++){
    47             if(dis[i]>max(dis[p],G[p][i])){
    48                 dis[i]=max(dis[p],G[p][i]);
    49             }
    50         }
    51     }        
    52 }
    53 
    54 int main(){
    55     int cnt=0;
    56     while(scanf("%d",&n)&&n){
    57     Getmap();
    58      Dijk();
    59     printf("Scenario #%d
    Frog Distance = %.3f
    
    ",++cnt,dis[2]);//这里G++不能是%.3lf 
    60     }
    61     return 0;
    62 }
  • 相关阅读:
    ORACLE权限管理
    ORACLE12.2.0.1.0 支持数据对象名称超过30个字符
    Redhat下卸载自带Mysql相关组键
    周鸿祎:如何做好产品经理
    javascript类型与类型检测
    AJAX
    HTTP协议
    HTML5web存储之localStorage
    CSS定位的三种机制:普通流、绝对定位和浮动
    CSS元素居中的常用方法
  • 原文地址:https://www.cnblogs.com/yzhhh/p/9982461.html
Copyright © 2011-2022 走看看