1. 程式人生 > >Frogger POJ-2253

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 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 0x3f3f3f3f

int 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; }