Frogger POJ - 2253 (改写最短路)

Frogger POJ - 2253

湖中有n块石头,编号从1到n,有两只青蛙,Bob在1号石头上,Alice在2号石头上,Bob想去看望Alice,但由于水很脏,他想避免游泳,于是跳着去找她。但是Alice的石头超出了他的跳跃范围。因此,Bob使用其他石头作为中间站,通过一系列的小跳跃到达她。两块石头之间的青蛙距离被定义为两块石头之间所有可能路径上的最小必要跳跃距离,某条路径的必要跳跃距离即这条路径中单次跳跃的最远跳跃距离。你的工作是计算Alice和Bob石头之间的青蛙距离。

Input

多实例输入
先输入一个整数n表示石头数量,当n等于0时结束。
接下来2-n+1行依次给出编号为1到n的石头的坐标xi , yi。
2 <= n <= 200
0 <= xi , yi <= 1000

Output

先输出"Scenario #x", x代表样例序号。
接下来一行输出"Frog Distance = y", y代表你得到的答案。
每个样例后输出一个空行。
(ps:wa有可能是精度问题,g++不对可以用c++尝试,都不对就是代码问题)

Examples

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




题意:

圈一下重点: 两块石头之间的青蛙距离被定义为两块石头之间所有可能路径上的最小必要跳跃距离,某条路径的必要跳跃距离即这条路径中单次跳跃的最远跳跃距离。你的工作是计算Alice和Bob石头之间的青蛙距离。

题解:

可见并不是普通的最短路, 仔细理解上述话, 意为取当前最短路径距离最大的某次边, 这样的话我们多判断一下max(d[i], cost[i][j]) 即可
另外仔细一点…开头不需要换行


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <queue>
#include <cmath>
using namespace std;
#define ms(x, n) memset(x,n,sizeof(x));
typedef  long long LL;
const LL maxn = 210;
const int inf = 1<<30;

int n;
struct V{
    int x, y;
    V(int xx, int yy){x=xx, y=yy;}
};
vector<V> v; //暂时存一下
double cost[maxn][maxn];
double calcu(double x1, double y1, double x2, double y2){
    return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}

double d[maxn];
bool used[maxn];
typedef pair<double, int> P;
void dijkstra(int s){
    ms(used, 0);
    fill_n(d, maxn, inf);
    priority_queue<P, vector<P>, greater<P> > q;
    q.push(P(d[s]=0, s));
    while(!q.empty()){
        P cur = q.top();
        q.pop();
        int i = cur.second;
        if(used[i]) continue;
        used[i] = true;

        for(int j = 0; j < n; j++)
            if(max(d[i], cost[i][j]) < d[j]){   //改写松弛操作
                d[j] = max(d[i], cost[i][j]);
                q.push(P(d[j], j));
            }
    }
}
int main()
{
    int t = 0;
    while(scanf("%d",&n)!=EOF, n){
        int x, y;
        v.clear();
        for(int i = 1; i <= n; i++){
            scanf("%d%d",&x,&y);
            v.push_back(V(x, y));
        }
        fill_n(cost[0], maxn*maxn, inf);
        for(int i = 0; i < n; i++)
            for(int j = i; j < n; j++)
                if(i!=j)
                    cost[i][j] = cost[j][i] = calcu(v[i].x, v[i].y, v[j].x, v[j].y);
        dijkstra(0);
        double ans = inf;

        if(t) printf("\n"); //开头不换行
        printf("Scenario #%d\nFrog Distance = %.3f\n", ++t, d[1]);
    }

	return 0;
}

猜你喜欢

转载自blog.csdn.net/a1097304791/article/details/87947490