Frogger

题目:

https://vjudge.net/problem/POJ-2253

注意这题的输出:
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.

对于每个测试用例,打印一行“Scenario #x”和一行“Frog Distance = y”,

其中 x 由测试用例编号(它们从 1 开始编号)替换,y 由适当的实数替换,打印到小数点后三位。

在每个测试用例之后放置一个空行,甚至在最后一个之后。

 

 

题意:

青蛙要从点1到点2,没法直接到,青蛙步是从点1 到点 2 的路上所有的边的最大值

要从点1到点2,生成树(只要1和2在一个集合里就可以了)的最大边

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
double x[203],y[203];
int f[203];
const int inf=0x3f3f3f3f;
struct edge
{
    int u,v;
    double w;
}e[40003];
int cmp(edge a,edge b)
{
    return a.w<b.w;
}
int find(int x)
{
    return x==f[x]?x:f[x]=find(f[x]);
}
int main()
{
    int n;
    int w=0;
    while(1)
    {
        scanf("%d",&n);
        if(n==0)
        { printf("\n");
              break;
        }
           w++;
        printf("Scenario #%d\n",w);
        for(int i=1;i<=n;i++)
        scanf("%lf %lf",&x[i],&y[i]);
        for(int i=1;i<=n;i++)
            f[i]=i;
            int c=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                e[c].u=i;
                e[c].v=j;
                e[c++].w=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
            }
        }
        double mmin=0;
        sort(e,e+c,cmp);
        for(int i=0;i<c;i++)
        {
            int x=e[i].u;
            int y=e[i].v;
            int fx=find(x);
            int fy=find(y);
            if(fx==fy)
            continue;
            else
            {
                f[fx]=fy;
                if(e[i].w>mmin)
                    mmin=e[i].w;
                if(find(1)==find(2))
                    break;
            }
        }
        printf("Frog Distance = %.3lf\n",mmin);
     printf("\n");
    }
}

 

上一篇:青蛙跳阶问题如何处理


下一篇:GOGOUP-11. 接口和多态