Frogger POJ - 2253(求两个石头之间”所有通路中最长边中“的最小边)

题意

? 题目主要说的是,有两只青蛙,在两个石头上,他们之间也有一些石头,一只青蛙要想到达另一只青蛙所在地方,必须跳在石头上。题目中给出了两只青蛙的初始位置,以及剩余石头的位置,问一只青蛙到达另一只青蛙所在地的所有路径中的“the frog distance”中的最小值。

? 解释一下“the frog distance”: 题目中给出了一段解释“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.” 其中 jump range 实际上就是指一条通路上的最大边,该词前面的minimum就说明了要求所有通路中最大边中的最小边。如果直接说前面这句话你可能感觉比较绕,通过上面的解释后我想你应该明白了吧。

? 通过上面的分析,不难看出这道题目的是求所有通路中最大边中的最小边,可以通过利用floyd,Dijkstra算法解决该题目,注意这道题可不是让你求两个点之间的最短路的,只不过用到了其中的一些算法思想。当然解决该题需要一个特别重要的方程,即

d[j] = min(d[j], max(d[x], dist[x][j]));        //dis[j]为从一号石头到第j号石头所有通路中最长边中的最小边

AC代码

利用Dijkstra算法

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll;
typedef long double ld;
int n;
const int maxn = 200 + 10;
const int inf = 0x3f3f3f3f;
int x[maxn], y[maxn], vis[maxn];
double dist[maxn][maxn], d[maxn];
double solve()
{
    memset(vis, 0, sizeof(vis));
    for(int i = 1;i <= n; i++)
        d[i] = dist[i][1];
    for(int i = 1; i <= n; i++)
    {
        int x;
        double minn = inf;
        for(int j = 1; j <= n; j++)
        {
            if(!vis[j] && d[j] < minn)
            {
                x = j;
                minn = d[j];
            }
        }
        vis[x] = 1;
        for(int j = 1; j <= n; j++)
            d[j] = min(d[j], max(d[x], dist[x][j]));        //dis[j]为从一号石头到第j号石头所有通路中最长边中的最小边
    }
    return d[2];
}

int main()
{
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
    int cnt = 0;
    while(scanf("%d", &n) && n)
    {
        for(int i = 1; i <= n; i++)
            scanf("%d %d", &x[i], &y[i]);
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= n; j++)
            {
                if(i == j)
                    dist[i][j] = 0;
                else
                    dist[i][j] = sqrt(double(x[i] - x[j])*(x[i] - x[j]) + double(y[i] - y[j])*(y[i] - y[j]));
            }
        }
        printf("Scenario #%d\n", ++cnt);
        printf("Frog Distance = ");
        printf("%.3lf\n\n",solve());
    }
}

原文地址:https://www.cnblogs.com/KeepZ/p/12199322.html

时间: 2024-10-08 01:29:59

Frogger POJ - 2253(求两个石头之间”所有通路中最长边中“的最小边)的相关文章

求两个日期之间间隔的天数,Python实现

代码如下 1 def leap_year(y): #判断是否是闰年 2 if (y % 4 == 0 and y % 100 != 0) or y % 400 == 0: 3 return True 4 else: 5 return False 6 7 def days_in_month(y, m): #判断每个月都有几天 8 if m in [1, 3, 5, 7, 8, 10, 12]: 9 return 31 10 elif m in [4, 6, 9, 11]: 11 return 30

js 求两个日期之间相差天数

//求两个日期之间的相差天数 function daysBetween(DateOne, DateTwo) { var OneMonth = DateOne.substring(5, DateOne.lastIndexOf('/')); var OneDay = DateOne.substring(DateOne.length, DateOne.lastIndexOf('/') + 1); var OneYear = DateOne.substring(0, DateOne.indexOf('/

B - Frogger POJ - 2253

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 rea

Frogger POJ 2253

http://poj.org/problem?id=2253 题意:有一只青蛙A想要跳到另一只青蛙B处, 由于青蛙A的跳跃幅度没有那么大, 所以它需要借助它周边的一些石头从而到达青蛙B处.问你在众多可供选择的道路中,哪条道路中跳跃的最大值要比其他道路中的最大值小.(也就是求它的跳跃幅度至少要为多少) #include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> #inclu

求两个年份之间闰年的数目

最开始碰到的是这个问题,求给定的两个数之间能同时被2,3,5整除的数,可以发现如果一个数能同时被2,3,5整除,那么它肯定可以被30整除.我们可以编写一个函数判断一个数是否可以被30整除,然后遍历给定的两个数中的区间逐个地判断就可以了. //解法一 bool isNums(int num) { if(num%30==0) return true; return false; } int calculateNums2(int begin,int end) { int count=0; for(in

floyd类型题UVa-10099-The Tourist Guide +Frogger POJ - 2253

The Tourist Guide Mr. G. works as a tourist guide. His current assignment is to take some tourists from one city to another. Some two-way roads connect the cities. For each pair of neighboring cities there is a bus service that runs only between thos

Frogger - poj 2253 (Dijkstra)

Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28802   Accepted: 9353 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 sinc

c++求两个日期之间的间隔天数

#include <iostream> using namespace std; static const unsigned char MonthTable[12] = {    31,28,31,30,31,30,31,31,30,31,30,31}; class Date{public:    Date(int year, int month, int day);    Date operator -(const Date& date);    //获取时间差    int get

POJ 2774 求两个串的最长公共前缀 | 后缀数组

#include<cstdio> #include<algorithm> #include<cstring> #define N 200005 using namespace std; int buf1[N],buf2[N],sa[N],rnk[N],buc[N],n,height[N],ans,belong[N]; char s[N]; void suffix_sort() { int *x=buf1,*y=buf2,m=1000; for (int i=0;i<