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

题意

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

解释一下“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算法解决该题目,注意这道题可不是让你求两个点之间的最短路的,只不过用到了其中的一些算法思想。固然解决该题须要一个特别重要的方程,即spa

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

AC代码

利用Dijkstra算法.net

#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());
    }
}
相关文章
相关标签/搜索