poj 2253 Frogger 【spfa跑一遍】

版权声明:转载的时候记着点个赞再评论一下! https://blog.csdn.net/LOOKQAQ/article/details/82790293

题目链接:http://poj.org/problem?id=2253

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 60900   Accepted: 19052

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这两只青蛙的距离,其他行的两个整数则代表着另外石头的坐标,青蛙1要跳到青蛙2处所有跳跃距离(包括不同的跳跃路线哦)最短时,当然,当石头多于3个时,就不能从石头1直接跳到石头2上了,现在让你求能在两块石头之间跳的最大距离(能到达的情况下)!

举例:第一组数组,给出了青蛙1和青蛙2的坐标,那么只有一种跳跃方案,输出距离为5;

第二组数据,青蛙1先跳到青蛙3处,跳跃距离为1.414,然后从青蛙3处跳到青蛙2处,跳跃距离还是1.414,以上是第一种方案,第二种方案是青蛙1直接跳到青蛙2,跳跃距离为2。1.414<2 输出1.414

给出汉语翻译:https://vjudge.net/contest/249818#problem/B

思路:贴上我的代码先(这道题的精度烦死人)

#include<math.h>
#include<iomanip> //setprecision头文件
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<queue>
using namespace std;
const int maxn = 1e3+10;
const int inf = 0x3f3f3f3f;
int vis[maxn];
double dis[maxn];
int n,m,u,v,w;
struct inp{
    int x;
    int y;
}p[maxn];
struct NODE{
    int e;
    double w;
    int next;
}edge[maxn*maxn];
int head[maxn],cnt;
void add(int u,int v,double w)
{
    edge[cnt].e = v;
    edge[cnt].w = w;
    edge[cnt].next = head[u];
    head[u] = cnt++;
}
void spfa() //spfa核心算法
{
    for(int i=1;i<=n;i++)
        dis[i] = inf;
    memset(vis,0,sizeof vis);
    queue<int>q;
    q.push(1);
    dis[1] = 0;
    vis[1] = 1;
    while(!q.empty())
    {
        int now = q.front();
        q.pop();
        vis[now] = 0;
        for(int i=head[now];i!=0;i=edge[i].next) //找啊找,不多说
        {
            int u = edge[i].e;
            if(dis[u] > max(dis[now],edge[i].w))
            {
                dis[u] = max(dis[now],edge[i].w);
                if(!vis[u])
                {
                    vis[u] = 1;
                    q.push(u);
                }
            }
        }
    }
}
int main()
{
    int cas=1;
    while(cin>>n && n)
    {
        memset(head,0,sizeof head); //初始化
        cnt = 1;
        for(int i=1;i<=n;i++)  //注意这一步的转化
        {
            cin>>p[i].x>>p[i].y;
            for(int j=1;j<i;j++)
            {
                double s = sqrt((p[i].x-p[j].x)*(p[i].x-p[j].x) + (p[i].y-p[j].y)*(p[i].y-p[j].y));
                add(i,j,s);
                add(j,i,s);
            }
        }
       spfa();
       //下面是输出,注重格式,不然卡死你
       cout<<"Scenario #"<<cas++<<endl;
       cout<<"Frog Distance = "<<fixed<<setprecision(3)<<dis[2]<<endl<<endl;
    }
    return 0;
}




猜你喜欢

转载自blog.csdn.net/LOOKQAQ/article/details/82790293