zoukankan      html  css  js  c++  java
  • POJ #2253 Frogger 变种Dijkstra

    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

    思路


      题目的意思是青蛙想从第一块石头跳到第二块石头,中间有许多垫脚石,求能跳到第二块石头的路上至少需要跳多远。拿第二个样例来说,顶点 1 到顶点 2 有两条路分别为:1(根号2)3(根号2)2 和 1(2)2 ,括号里的值表示相邻两点的距离。其中前一条路青蛙至少得具有根号2的跳跃能力才能到达第二块石头,后一条路需要具有2的跳跃能力。由于两条路都能到达顶点2,那么青蛙的跳跃能力只需要根号2即可。

      实质上,这道题就是求所有通路的最大边的最小值。

      我们让 d 数组中存储所有从顶点1到顶点v的可达路径中最大边的最小值,那么依旧可以使用 Dijkstra 贪心得到顶点1到顶点2的可达路径中最大边的最小值。由于比较的是某条边的权,所以需要将松弛条件改成如下形式:

    d[v] > max( d[u], w(u, v) )

    AC代码:

    #include<iostream>
    #include<algorithm>
    #include<cstring>
    #include<cmath>
    #include<iomanip>
    using namespace std;
    #define INF 0x3f3f3f3f
    const int MAX_N = 210;
    int n;
    int x[MAX_N], y[MAX_N];
    
    //u点到v点的距离
    double w (int u, int v) {
        return sqrt((double)(x[v] - x[u])*(x[v] - x[u]) + (y[v] - y[u])*(y[v] - y[u]));
    }
    
    double d[MAX_N];
    bool vis[MAX_N];
    void dijkstra (const int& s) {
        for (int i = 1; i <= n; ++i)  d[i] = INF, vis[i] = false;
        d[1] = 0;
    
        for (int i = 1; i <= n; ++i) {
            double min_d = INF;
            int u;
            for (int j = 1; j <= n; ++j ) {
                if ( !vis[j] && d[j] < min_d ) {
                    min_d = d[j];
                    u = j;
                }
            }
            vis[u] = true;
            //图为无向图,所有顶点都有 n-1 条邻接边
            for (int v = 1; v <= n; ++v) {
                if ( d[v] > max (d[u], w(u, v) ) ) {
                    d[v] = max (d[u], w(u, v) );
                }
                //d[v] = min (d[v], max(d[u], w(u, v)));
            }
        }
    }
    
    int main (void) {
        int test_num = 0;
        while (cin >> n && n ) {
            for (int i = 1; i <= n; ++i) {
                cin >> x[i] >> y[i];
            }
            dijkstra (1);
            cout << "Scenario #" << ++test_num << endl
                 << "Frog Distance = " << setiosflags(ios::fixed) << setprecision(3) << d[2] << endl << endl;
        }
        return 0;
    }
    View Code

      

    ————全心全意投入,拒绝画地为牢
  • 相关阅读:
    将view添加到地图覆盖物
    将eclipse的编码改成UTF-8,默认是GBK
    Android调用系统关机与重启功能
    Android中preference标签的使用
    linux中convert用法
    RookeyFrame在线新增模块
    RookeyFrame2.0发布,UI重构
    Rookey.Frame v1.0 视频教程发布了
    Rookey.Frame企业级快速开发框架开源了
    RookeyFrame模块初始化
  • 原文地址:https://www.cnblogs.com/Bw98blogs/p/8455405.html
Copyright © 2011-2022 走看看