[Kuangbin take you to fly] four thematic shortest practice B (POJ 2253) Frogger (spfa)

B - Frogger(spfa)

Topic links: https://vjudge.net/contest/66569#problem/B

topic:

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
#. 1 Scenario 
Frog Distance = 5.000 

Scenario # 2 
Frog Distance = 1.414 that Italy: there are two frogs and several stones, these things are now known coordinates, the coordinates of two frogs A and B coordinates frog first and second coordinates, now frog a frog wants to go to B, and a frog jumps can make use of any stone, but there are a number of paths from a to B, ask the biggest edge on all paths from a to B, for example, there are two pathway 1 (4) 5 (3) represents between sides 4 1-5, 5-2 between the sides 3, the article passage jump range (the maximum distance between two stones) 4 ,  other path 1 (6) 4 (1) 2, path hopping scope of the article 6, the range of the jump two paths 4, 6, respectively, we require is that a minimum jump range, i.e. 4, with three methods can be resolved idea: the shortest path problem template, change a little bit, is the largest of each path chosen, select the smallest of which several paths, that is the template d [i]> d [now ] + lu [ now] [i] into d [i]> max (d [now], lu [now] [i]) on the line, WA many times due to the initialization, after the beginning of Or try to initialize a cycle of it, spfa algorithm code as follows:









//
// Created by hanyu on 2019/7/14.
//
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<cstring>
#include<cstdio>
#include<math.h>
using namespace std;
typedef long long ll;
#define MAX 0x3f3f3f3f
const int maxn=1005;
double d[maxn];
double lu[maxn][maxn];
bool book[maxn];
int n;
void spfa(int A) 
{ 
    for ( int I = 2 ; I <= n-; I ++ ) { 
        D [I] = MAX; 
        Book [I] = to false ; 
    } 
    // Memset (Book, to false, the sizeof (Book))
     // Memset (D, MAX, the sizeof (D))
     // do initialization with the above comments, WA because of this many times, for two hours to find error 
    Queue < int > Qu; 
    D [a] = to false ; 
    Book [ A] = to true ; 
    qu.push (A); 
    int now;
     the while (! qu.empty ()) 
    { 
        now=qu.front();
        qu.pop();
        book[now]=false;
        for(int i=1;i<=n;i++)
        {
            if(d[i]>max(d[now],lu[now][i]))
            {
                d[i]=max(d[now],lu[now][i]);
                if(!book[i])
                {
                    qu.push(i);
                    book[i]=true;
                }
            }

        }
    }
}
int main()
{
    int k=0;
    int a[maxn],b[maxn];
    while(~scanf("%d",&n)) {
        if (n == 0)
            break;

        for (int i = 1; i <= n; i++) {
            scanf("%d%d", &a[i], &b[i]);

        }
        for (int i = 1; i <= n; i++){
            for (int j = 1; j <= i; j++) {
                lu[i][j] = lu[j][i] = sqrt(double(a[i] - a[j]) * (a[i] - a[j]) +double(b[i] - b[j]) * (b[i] - b[j]));

            }
        }
        spfa(1);
        k++;
        printf("Scenario #%d\n",k);
        printf("Frog Distance = %.3lf\n\n",d[2]);
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/Vampire6/p/11202573.html