当前位置: 代码迷 >> 综合 >> Frogger POJ-2253
  详细解决方案

Frogger POJ-2253

热度:13   发布时间:2023-12-29 17:34:01.0

题目链接:POJ-2253

[kuangbin带你飞]专题六最短路

题目描述

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.

思路

Dijkstra算法:
复制一下别人的题意,有两只青蛙和若干块石头,现在已知这些东西的坐标,两只青蛙A坐标和青蛙B坐标是第一个和第二个坐标,现在A青蛙想要到B青蛙那里去,并且A青蛙可以借助任意石头的跳跃,而从A到B有若干通路,问从A到B的所有通路上的最大边,比如有 有两条通路 1(4)5 (3)2 代表1到5之间的边为4, 5到2之间的边为3,那么该条通路跳跃范围(两块石头之间的最大距离)为 4, 另一条通路 1(6) 4(1) 2 ,该条通路的跳跃范围为6, 两条通路的跳跃范围分别是 4 ,6,我们要求的就是最小的那一个跳跃范围,即4,

代码


#include <iostream>
#include <cstring>
#include <cmath>
#include <iomanip>
using namespace std;#define MAX 210
#define INF 0x3f3f3f3fint x[MAX], y[MAX], vis[MAX];
double mat[MAX][MAX], dist[MAX];void init(int n)
{
    memset(vis, 0, sizeof(vis));for(int i = 1; i<=n; i++){
    for(int j = 1; j<=n; j++)mat[i][j] = INF;dist[i] = INF;}
}void Dijkstra(int n, int v)
{
    dist[v] = 0;for(int i = 1; i<=n; i++){
    int min_vertex;double min_dist = INF;for(int j = 1; j<=n; j++){
    if(!vis[j] && dist[j] < min_dist){
    min_dist = dist[j];min_vertex = j;}}vis[min_vertex] = 1;for(int j = 1; j<=n; j++){
    if(!vis[j])dist[j] = min(dist[j], max(min_dist , mat[min_vertex][j]));}}
}int main()
{
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);int n, t = 0;while(cin >> n, n){
    t++;init(n);for(int i = 1; i<=n; i++)//录入顶点坐标{
    cin >> x[i] >> y[i];}for(int i = 1; i<=n; i++){
    for(int j = i; j<=n; j++){
    mat[i][j] = mat[j][i] = sqrt(pow(x[i]-x[j],2) + pow(y[i]-y[j], 2));//cout << "i = " << i << "j = " << j << " mat[i][j] = " << mat[i][j] << endl;}}Dijkstra(n, 1);cout << "Scenario #" << t << endl;cout << "Frog Distance = " << fixed << setprecision(3) << dist[2] << endl << endl;}return 0;
}