zoukankan      html  css  js  c++  java
  • poj 2253 Frogger 最小瓶颈路(变形的最小生成树 prim算法解决(需要很好的理解prim))

    传送门:

    http://poj.org/problem?id=2253

    Frogger
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 58328   Accepted: 18293

    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
    
    分析:
    题目意思:青蛙a在石头1上,青蛙b在石头2上,青蛙a要去找青蛙b玩,问你从从石头1到石头2的最短路里最大的距离是多少

    做法:
    可以用prim写,但是prim是求MST的,这样只要求1到2的最短路上的最大边就可以了
    找j的时候,找到了2就跳出去
    注意:
    代码里面是0号石头 ,1号石头,而不是分析中的1号,2号石头

    code
    #include <iostream>
    #include <cstdio>
    #include<stdio.h>
    #include<algorithm>
    #include<cstring>
    #include<math.h>
    #include<memory>
    using namespace std;
    typedef long long LL;
    #define INF 0x3f3f3f3f
    #define max_v 205
    struct node
    {
        double x,y;
    }p[max_v];
    double g[max_v][max_v];
    int n,c=1;
    void init()
    {
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
            g[i][j]=INF;
    }
    void prim(int s)
    {
        double lowcost[n];
        int used[n];
        for(int i=0;i<n;i++)
        {
            lowcost[i]=g[s][i];
            used[i]=0;
        }
        double ans=0;
        used[s]=1;
        for(int i=1;i<n;i++)
        {
            int j=0;
            for(int k=0;k<n;k++)
            {
                if(!used[k]&&lowcost[k]<lowcost[j])
                    j=k;
            }
            if(j==0)
                break;
            ans=max(ans,lowcost[j]);
            used[j]=1;
            if(j==1)
                break;
            for(int k=0;k<n;k++)
            {
                if(!used[k]&&g[j][k]<lowcost[k])
                {
                    lowcost[k]=g[j][k];
                }
            }
        }
        printf("Scenario #%d
    ",c++);
        printf("Frog Distance = %0.3f
    
    ",ans);
    }
    double f(int i,int j)
    {
        double x=p[i].x-p[j].x;
        double y=p[i].y-p[j].y;
        return sqrt(x*x+y*y);
    }
    int main()
    {
        while(~ scanf("%d",&n))
        {
            if(n==0)
                break;
            for(int i=0;i<n;i++)
            {
                scanf("%lf %lf",&p[i].x,&p[i].y);
            }
            init();
            for(int i=0;i<n;i++)
            {
                for(int j=i+1;j<n;j++)
                {
                    g[i][j]=g[j][i]=f(i,j);
                }
            }
            prim(0);
        }
        return 0;
    }
  • 相关阅读:
    PHP学习—Cookie&Session
    IP组播综合实验
    k8s kubeadm部署高可用集群
    python调用zbbix的api实现批量添加域名监控脚本
    Python调用Harbor api删除私有仓库harbor镜像
    Kubernetes故障排查指南-分析容器退出状态码
    blackbox_exporter+grafana+prometheus监控主机存活,端口存活及网站状态
    k8s中使用ceph-csi在ceph中进行数据持久化
    Jenkins+K8S流水线自动化部署Java程序
    生产环境k8s中使用helm部署prometheus+grafana监控k8s集群中相关node和pod
  • 原文地址:https://www.cnblogs.com/yinbiao/p/9432976.html
Copyright © 2011-2022 走看看