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

    Frogger
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 26519   Accepted: 8633

    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

    二分最大距离 + 记忆化dfs一条可行路...

    #include <iostream>
    #include <vector>
    #include <queue>
    #include <cmath>
    #include <algorithm>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    typedef long long LL;
    const int N = 210;
    const int inf = 1e9+7 ;
    const double eps = 1e-7;
    int n , m ;
    double dis[N][N] , x[N] , y[N];
    inline double DIS( int a , int b ){ return sqrt((x[a]-x[b])*(x[a]-x[b]) + (y[a]-y[b])*(y[a]-y[b]));}
    bool vis[N] ;
    struct node
    {
        int v ;
        double  w;
        node(){};
        node( int a , double b ) { v =a , w = b; }
    };
    vector<node>g[N];
    
    bool tag ;
    void dfs( int u ,double DIS )
    {
        if( vis[u] || tag ) return ;
        if( u == 2 ) { tag = true ; return  ; }
        vis[u] = true ;
        for( int i = 0 ; i < g[u].size(); ++i ){
            int v = g[u][i].v ;
            double w = g[u][i].w ;
            if(  DIS + eps < w  ) continue ;
            dfs( v , DIS );
        }
    }
    
    bool test(  double DIS )
    {
        tag = false;
        memset( vis , false , sizeof vis );
        dfs( 1,  DIS );
        if( tag ) return true;
        return false;
    }
    
    void run()
    {
        for( int i = 0 ; i <= n ; ++i )  g[i].clear() ;
        for( int i = 1 ; i <= n ; ++i ) cin >> x[i] >> y[i] ;
        for( int i = 1 ; i <= n ; ++i ){
            for( int j = i + 1 ; j <= n ; ++j ){
                double w = DIS( i , j );
                g[i].push_back(node(j,w));
                g[j].push_back(node(i,w));
            }
        }
        double ans = 0 , l = 0.0 , r = DIS(1,2) + 10.0;
        while( l + eps <= r ){
            double mid = (l+r) / 2.0 ;
            if( !test(mid) )
                l = mid + eps ;
            else
                ans = mid , r = mid - eps ;
        }
        printf("%.3lf
    
    ",ans);
    }
    
    int main()
    {
        #ifdef LOCAL
            freopen( "in.txt","r",stdin );
        #endif // LOCAL
        ios::sync_with_stdio(false);
        int cas = 1 ;
        while( cin >> n && n ) cout << "Scenario #"<< cas++ <<endl << "Frog Distance = "  ,  run();
    }
    View Code
    only strive for your goal , can you make your dream come true ?
  • 相关阅读:
    数据结构
    Lucene基础入门
    mybatis关系表
    ** WARNING ** : Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package.
    springboot 运行出现错误 Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
    Django的项目创建,以及该端口号,语言随地区而变化
    命令框下上传到gitee
    如何实现windows命令提示符的tab补全
    git 常见命令
    与系统环境变量重名的解决办法
  • 原文地址:https://www.cnblogs.com/hlmark/p/4069976.html
Copyright © 2011-2022 走看看