J - 最小瓶颈路 UVA - 534 Frogger

J - 最小瓶颈路

 UVA - 534

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 file 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~n个点,现在要从1跳到2,问每次跳跃的最大距离的最小值是多少

思路:按照Kruskal的求最小生成树的步骤,可知在求最小生成树的时候,每两个点之间的距离 虽然不一定是最短的,但是每两个直接相连的点之间的距离一定是最短的,题目就相当于求所走的路线中直接相连的两个点之间的距离的最大值的最小值。

所以直接按照Kruskal求最小生成树的步骤来求就好了,只是不用把最小生成树求出来,当1与2 连接的时候就可以了,此时加入的最后一条边就是答案。

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <vector>
#include <malloc.h>
#define Twhile() int T;scanf("%d",&T);while(T--)
#define clc(a,b,n) for(int i=0;i<=n;i++)a[i]=b
#define clc2(a,b,n,m) for(int i=0;i<=n;i++)for(int j=0;j<=m;j++)a[i][j]=b
#define fora(i,a,b) for(int i=a;i<b;i++)
#define fors(i,a,b) for(int i=a;i>b;i--)
#define fora2(i,a,b) for(int i=a;i<=b;i++)
#define fors2(i,a,b) for(int i=a;i>=b;i--)
#define PI acos(-1.0)
#define eps 1e-6
#define INF 0x3f3f3f3f
#define BASE 131

typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
using namespace std;
const int maxn=200+11;
int N,M;
int xx[maxn],yy[maxn];
struct edge
{
    int u,v;//点u到v
    double var;//权值
}ma[maxn*maxn];
bool cmp(edge a,edge b)
{
    return a.var<b.var;
}
int fa[maxn];
void initKruskal()
{
    fora2(i,1,N)
    {
        fa[i]=i;
    }
    sort(ma+1,ma+M+1,cmp);

}
int findx(int x)
{
    if(x==fa[x])return x;
    return fa[x]=findx(fa[x]);
}
bool unio(int x,int y)
{
    int fx=findx(x),fy=findx(y);
    if(fx==fy)return false;
    fa[fy]=fx;
    return true;
}
void kruskal()
{
    initKruskal();
    int m=0;
    fora2(i,1,M)
    {
        unio(ma[i].u,ma[i].v);
        if(findx(1)==findx(2))
        {
            printf("%.3f\n\n",ma[i].var);
            return;
        }
    }
}
int main()
{
    int t=0;
    while(~scanf("%d",&N)&&N)
    {
        fora2(i,1,N)scanf("%d%d",xx+i,yy+i);
        int k=1;
        fora2(i,1,N)
        {
            fora2(j,1,i)
            {
                ma[k].u=i;
                ma[k].v=j;
                ma[k].var=sqrt((xx[i]-xx[j])*(xx[i]-xx[j])+(yy[i]-yy[j])*(yy[i]-yy[j]));
                k++;
            }
        }
        M=k;
        printf("Scenario #%d\nFrog Distance = ",++t);
        kruskal();
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/liyang__abc/article/details/81560078