zoukankan      html  css  js  c++  java
  • POJ

    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

    这道题跟poj-1797可以一起做,对比一下。
    大致意思是求青蛙从1跳到2时最大的跳跃边,也就是最小生成树的最大边问题。(当然dijkstra和spfa也能做,但个人认为kruskal好想一点)
    附代码:
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 #include <queue>
     5 #include <cmath>
     6 using namespace std;
     7 const int M = 2222;
     8 int pre[M],rk[M];
     9 int V,E;
    10 struct nod{
    11     int u,v;
    12     double cost;
    13 }eg[M*M/2];
    14 bool cmp(const nod&a,const nod&b){
    15     return a.cost<b.cost;
    16 }
    17 struct stu{
    18     double x,y;
    19 }coo[M*M/2];
    20 void init(int V){
    21     for(int i=0;i<=V;i++){
    22         rk[i]=0;
    23         pre[i]=i;
    24     }
    25 }
    26 int find(int x){
    27     if(pre[x]==x){
    28         return x;
    29     } else{
    30         return pre[x]=find(pre[x]);
    31     }
    32 }
    33 void mix(int x,int y){
    34     x=find(x);
    35     y=find(y);
    36     if(x==y) return;
    37     if(rk[x]<rk[y]){
    38         pre[x]=y;
    39     }
    40     else{
    41         pre[y]=x;
    42         if(rk[x]==rk[y]){
    43             rk[x]++;
    44         }
    45     }
    46 }
    47 int cnt=0;
    48 void kruskal(){
    49     
    50     sort(eg,eg+E,cmp);
    51     //printf("%d
    ",E);
    52     for(int i=0;i<E;i++){
    53         nod e=eg[i];
    54         if(find(e.u)!=find(e.v)){
    55 
    56             mix(e.u,e.v);
    57             if(find(0)==find(1)){
    58             cnt=i;
    59             break;
    60             }
    61           //  if(cnt==V-1) return e.cost;
    62         }
    63         
    64     }
    65     return ;
    66 }
    67 int main(){
    68     int cas=0;
    69     while(scanf("%d",&V),V){
    70         E=0;
    71         init(V);
    72         for(int i=0;i<V;i++){
    73             scanf("%lf %lf",&coo[i].x,&coo[i].y);
    74         }
    75         for(int i=0;i<V-1;i++){
    76             for(int j=i+1;j<V;j++){
    77                 double xx=coo[i].x-coo[j].x;
    78                 double yy=coo[i].y-coo[j].y;
    79                 eg[E].u=i;
    80                 eg[E].v=j;
    81                 eg[E++].cost=xx*xx+yy*yy;
    82             }
    83         }
    84         kruskal();
    85         printf("Scenario #%d
    Frog Distance = %.3lf
    
    ",++cas,sqrt(eg[cnt].cost));
    86     }
    87     return 0;
    88 }
    View Code

  • 相关阅读:
    Django 用ModelForm批量保存form表单(非常实用的方法) mfor_verity项目
    jquery ajax异步提交表单数据的方法
    python字符串转换成变量的几种方法
    django 线上线下使用不同的数据库 上线:mysql 线下sqlite3 以及debug模式的开和关
    django admin 或xdmin list_display search_fields list_filter 如果显示搜索外键或多对多字段
    nonce和timestamp在Http安全协议中的作用
    Web API接口 安全验证
    .Net环境下的缓存技术介绍
    .Net缓存管理框架CacheManager
    在asp.net web api中利用过滤器设置输出缓存
  • 原文地址:https://www.cnblogs.com/zmin/p/7427237.html
Copyright © 2011-2022 走看看