zoukankan      html  css  js  c++  java
  • POJ 2235 Frogger / UVA 534 Frogger /ZOJ 1942 Frogger(图论,最短路径)

    POJ 2235 Frogger / UVA 534 Frogger /ZOJ 1942 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

    Http

    POJ:https://vjudge.net/problem/POJ-2253
    UVA:https://vjudge.net/problem/UVA-534
    ZOJ:https://vjudge.net/problem/ZOJ-1942

    Source

    图论,最短路径

    题目大意

    求两点之间的一条路径满足这条路径上最大的边权最小

    解决思路

    运用改进的spfa算法就可以了

    代码

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<algorithm>
    #include<vector>
    #include<cmath>
    #include<queue>
    using namespace std;
    
    #define ll long long
    
    const int maxN=3000;
    const int inf=147483647;
    
    class Edge
    {
    public:
        int v;
        ll w;
    };
    
    int n,m;
    vector<Edge> E[maxN];
    ll Dist[maxN];
    int Px[maxN];
    int Py[maxN];
    bool inqueue[maxN];
    queue<int> Q;
    
    int main()
    {
        int ti=0;
        while (cin>>n)
        {
            if (n==0)
                break;
            for (int i=1;i<=n;i++)
            {
                E[i].clear();
            }
            memset(Dist,-1,sizeof(Dist));
            for (int i=1;i<=n;i++)
                cin>>Px[i]>>Py[i];
            for (int i=1;i<=n;i++)
                for (int j=i+1;j<=n;j++)
                {
                    ll dist=(Px[i]-Px[j])*(Px[i]-Px[j])+(Py[i]-Py[j])*(Py[i]-Py[j]);
                    E[i].push_back((Edge){j,dist});
                    E[j].push_back((Edge){i,dist});
                }
            memset(inqueue,0,sizeof(inqueue));
            while (!Q.empty())
                Q.pop();
            Dist[1]=0;
            inqueue[1]=1;
            Q.push(1);
            do
            {
                int u=Q.front();
                Q.pop();
                inqueue[u]=0;
                for (int i=0;i<E[u].size();i++)
                {
                    int v=E[u][i].v;
                    if ((max(Dist[u],E[u][i].w)<Dist[v])||(Dist[v]==-1))
                    {
                        Dist[v]=max(Dist[u],E[u][i].w);
                        if (inqueue[v]==0)
                        {
                            Q.push(v);
                            inqueue[v]=1;
                        }
                    }
                }
            }
            while (!Q.empty());
            ti++;
            printf("Scenario #%d
    Frog Distance = %.3f
    
    ",ti,sqrt(Dist[2]*1.0));
            //cout<<Dist[2]<<endl;
        }
        return 0;
    }
    
  • 相关阅读:
    String字符串性能优化的探究
    Spring笔记(5)
    Spring笔记(4)
    力扣217. 存在重复元素
    力扣1480. 一维数组的动态和
    Ribbon 和 Feign 的区别
    Eureka的自我保护机制
    Planar graph and map 3-colorability reduce to one another
    3-colorability
    一少年独坐屋中突然顿悟!-回溯
  • 原文地址:https://www.cnblogs.com/SYCstudio/p/7222887.html
Copyright © 2011-2022 走看看