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

    简单的最短路,二分+dij判断
    #include<map>
    #include<set>
    #include<stack>
    #include<queue>
    #include<cmath>
    #include<vector>
    #include<cstdio>
    #include<string>
    #include<cstring>
    #include<cstdlib>
    #include<iostream>
    #include<algorithm>
    #define  inf 0x0f0f0f0f
    using namespace std;
    
    const int MAX=200+10;
    const double esp=1e-8;
    
    typedef pair<int,int>pii;
    priority_queue<pii,vector<pii>,greater<pii> >q;
    
    struct node
    {
        int id,cost;
    };
    vector<node> stone[MAX];
    
    int dist[MAX],n;
    bool vis[MAX];
    double dis[MAX][MAX],xx[MAX],yy[MAX];
    
    void dijkstra(int k)
    {
        for(int i=0;i<=n;i++) dist[i]=(i==k? 0 : 99999999);
        memset(vis,0,sizeof(vis));
    
        q.push(make_pair(dist[k],k));
        while(!q.empty())
        {
            pii u=q.top();
            q.pop();
            int x=u.second;
            if(!vis[x])
            {
                vis[x]=true;
                for (int i=0;i<stone[x].size();i++)
                {
                    if(dist[stone[x][i].id]>dist[x]+stone[x][i].cost)
                    {
                        dist[stone[x][i].id]=dist[x]+stone[x][i].cost;
                        q.push(make_pair(dist[stone[x][i].id],stone[x][i].id));
                    }
                }
            }
        }
    }
    
    void get_dist()
    {
        for (int i=1;i<=n;i++)
        for (int j=1;j<=n;j++)
        {
            dis[i][j]=sqrt((xx[i]-xx[j])*(xx[i]-xx[j])+(yy[i]-yy[j])*(yy[i]-yy[j]));
        }
    }
    
    void update(double st)
    {
        for (int i=0;i<=n;i++) stone[i].clear();
        for (int i=1;i<=n;i++)
        for (int j=1;j<=n;j++)
        {
            if(dis[i][j]-st>esp)
            {
                node temp;
                temp.id=j; temp.cost=1;
                stone[i].push_back(temp);
                temp.id=i;
                stone[j].push_back(temp);
            }
            else
            {
                node temp;
                temp.id=j; temp.cost=0;
                stone[i].push_back(temp);
                temp.id=i;
                stone[j].push_back(temp);
            }
        }
    }
    
    double find(double x,double y)
    {
        while (y-x>esp)
        {
            double m=x+(y-x)/2;
            update(m);
            dijkstra(1);
            if (dist[2]==0) y=m;
            else x=m;
        }
        return x;
    }
    
    int main()
    {
        int caset=0;
        while (scanf("%d",&n)!=EOF && n)
        {
            caset++;
            for (int i=1;i<=n;i++)
            {
                scanf("%lf%lf",&xx[i],&yy[i]);
            }
            get_dist();
            double ans=find(0,dis[1][2]);
            printf("Scenario #%d
    ",caset);
            printf("Frog Distance = %.3lf
    
    ",ans);
        }
        return 0;
    }
    至少做到我努力了
  • 相关阅读:
    Linux异常现场--pt_regs浅析
    内核调试--确认结构体的size和结构体成员的偏移
    Linux内核中root_domain的建立与初始化
    solr学习笔记-全量更新与增量更新,去除html标签
    oracle 替换clob里面的某个特定的字符串
    oracle重置序列从1开始
    oracle提取汉字拼音首字母
    layer.open的yes函数中获取弹出层(子集iframe)中的元素或参数
    java.util.Date日期时间工具类
    js操作将数字转换成 , 逗号分割的字符串并追加‘万’字
  • 原文地址:https://www.cnblogs.com/chensunrise/p/3745871.html
Copyright © 2011-2022 走看看