zoukankan      html  css  js  c++  java
  • Frogger

    POJ - 2253 
    题目意思大体就是找一条路,这条路中每一跳都尽量的小。找到这条路中最大的边

    题目大意求青蛙由起点跳到终点的过程中,在所有的路径中的最大步伐中的最小步伐。题目简单。用Dijkstra算法解,但并非是求最小路径或最  

    小路径长度。这里是Dijkstra算法的变体,结合贪心算法的思想,在走每一步时,选取距离最小(即步伐最小)的那一步走,每一次走都是在上一步  

    的基础之上走的,用一个变量u记录每次通过的结点,再用一个变量记录已经走了的路径中的最大步伐,当u等于终点结点的编号时,就停止,输出最大步伐。

     1 #include<iostream>
     2 #include<algorithm>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<cstdio>
     6 using namespace std;
     7 
     8 const int INF=0x3f3f3f3f;
     9 int book[1005];
    10 double dis[1005],e[1005][1005];
    11 
    12 struct note
    13 {
    14     int x,y;
    15 }p[1005];
    16 
    17 double length(note a,note b)
    18 {
    19     return sqrt( (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y) );
    20 }
    21 
    22 int main()
    23 {
    24     int n,cas=0;
    25     while(cin>>n && n)
    26     {
    27         cas++;
    28         memset(book,0,sizeof(book));
    29         memset(dis,0,sizeof(dis));
    30         for(int i=1;i<=n;i++)
    31             for(int j=1;j<=n;j++)
    32                 e[i][j]=INF;
    33         for(int i=1;i<=n;i++)
    34             cin>>p[i].x>>p[i].y;
    35         
    36         for(int i=1;i<=n;i++)
    37             for(int j=1;j<=n;j++)
    38                 e[i][j] = length(p[i],p[j]);
    39                 
    40         for(int i=1;i<=n;i++)
    41             dis[i]= e[1][i];
    42         dis[1]=0;
    43         book[1]=1;
    44         
    45         double maxn=0;
    46         for(int i=2;i<=n;i++)
    47         {
    48             double minn=INF;
    49             int u=1; //******
    50             for(int j=1;j<=n;j++)
    51             {
    52                 if( !book[j] && dis[j]<minn)
    53                 {
    54                     minn=dis[j];
    55                     u=j;
    56                 }
    57             }
    58             
    59             if(maxn<minn)  maxn=minn;
    60             if(u==2) break; //如果最短的是第2个点  则直接就到目的地了 
    61             book[u]=1;
    62             
    63             for(int v=1;v<=n;v++)
    64             {
    65                 if(!book[v] && dis[v]> e[u][v])
    66                     dis[v]= e[u][v];
    67             }
    68         }
    69         
    70         printf("Scenario #%d
    Frog Distance = %.3f
    
    ",cas,maxn);
    71     }
    72 }


  • 相关阅读:
    P3478 [POI2008]STA-Station
    P2015 二叉苹果树
    P2014 选课 (树型背包模版)
    求树的每个子树的重心
    求树的直径
    Javascript--防抖与节流
    JavaScript中call和apply的区别
    解决谷歌浏览器“此Flash Player与您的地区不相容,请重新安装Flash”问题(最新版)
    matlab实验代码(总)
    表达式树
  • 原文地址:https://www.cnblogs.com/thunder-110/p/9114208.html
Copyright © 2011-2022 走看看