码迷,mamicode.com
首页 > 其他好文 > 详细

poj 2253 Frogger(最小生成树)

时间:2015-07-25 15:17:16      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:

poj 2253 Frogger

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 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.

Input
The input will contain one or more test cases. The first line of each test case will contain the number of stones n (2<=n<=200). The next n lines each contain two integers xi,yi (0 <= xi,yi <= 1000) representing the coordinates of stone #i. Stone #1 is Freddy’s stone, stone #2 is Fiona’s stone, the other n-2 stones are unoccupied. There’s a blank line following each test case. Input is terminated by a value of zero (0) for n.

Output
For each test case, print a line saying “Scenario #x” and a line saying “Frog Distance = y” where x is replaced by the test case number (they are numbered from 1) and y is replaced by the appropriate real number, printed to three decimals. Put a blank line after each test case, even after the last one.

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

题目大意:给你n块石头的坐标,有两只青蛙分别在一号石头和二号石头上。一号青蛙想去找二号青蛙,求他所走的最短路上的最大跳跃距离是多少。

解题思路:最小生成树。Prim和Kruskal都可以。

Kruskal算法

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
using namespace std;
typedef long long ll;
const int N = 300;
const int M = 40005;
const int INF = 0x3f3f3f3f;
struct Node{
    int x, y;
    double len;
}q[M];
int fa[M];
int n, cnt;
double X[N], Y[N];
void init() {
    cnt = 0;
    for (int i = 0; i < M; i++) fa[i] = i;
    memset(X, 0, sizeof(X));
    memset(Y, 0, sizeof(Y));
}
int find(int x) {
    return x == fa[x] ? x : fa[x] = find(fa[x]);
}
double getlen(int a, int b) {
    return sqrt((X[a] - X[b]) * (X[a] - X[b]) + (Y[a] - Y[b]) * (Y[a] - Y[b])); 
}
int cmp(Node x, Node y) {
    return x.len < y.len;
}
void input() {
    for (int i = 0; i < n; i++) {
        scanf("%lf %lf", &X[i], &Y[i]); 
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (i == j) continue;
            q[cnt].x = i;
            q[cnt].y = j;
            q[cnt++].len = getlen(i, j);
        }   
    }
}
double kruskal() {
    sort(q, q + cnt, cmp);
    double ans = 0;
    for (int i = 0; i < cnt; i++) {
        int x = find(q[i].x), y = find(q[i].y);
        if (x != y) {
            fa[x] = y;
            if (find(0) == find(1)) {
                ans = q[i].len;
                break;
            }
        }
    }
    return ans;
}
int main() {
    int Case = 1;
    while (scanf("%d", &n) != EOF, n) {
        printf("Scenario #%d\n", Case++);
        printf("Frog Distance = ");
        init(); 
        input();
        printf("%.3lf\n\n", kruskal());
    }
    return 0;
}

Prim算法

#include <cstdio> 
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
using namespace std;
typedef long long ll;
const double INF = 0x3f3f3f3f;
const int N = 300;
int n;
double D[N][N], X[N], Y[N], ans, dis[N];
double Dis(int x, int y) {
    return sqrt((X[x] - X[y]) * (X[x] - X[y]) + (Y[x] - Y[y]) * (Y[x] - Y[y]));
}
void init() {
    ans = 0;
    memset(X, 0, sizeof(X));
    memset(Y, 0, sizeof(Y));
    memset(D, 0, sizeof(D));
    memset(dis, 0, sizeof(dis));
}
void input() {
    for (int i = 0; i < n; i++) {
        scanf("%lf %lf", &X[i], &Y[i]); 
    }           
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (i == j) continue;   
            D[i][j] = Dis(i, j);
        }   
    }
}
void Prim() {
    int m;
    for (int i = 0; i < n; i++) {
        dis[i] = D[0][i];
    }
    for (int i = 0; i < n; i++) {
        double L = INF;
        for (int j = 0; j < n; j++) {
            if (dis[j] && dis[j] < L) {
                L = dis[j];
                m = j;
            }   
        }
        if (ans < L && L != INF) {
            ans = L;    
        }
        if (m == 1) break;
        for (int j = 0; j < n; j++) {
            if (D[j][m] < dis[j]) {
                dis[j] = D[j][m];
            }   
        }
    }
    printf("%.3lf\n\n", ans);
}
int main() {
    int Case = 1;
    while (scanf("%d", &n) != EOF, n) {
        printf("Scenario #%d\n", Case++);
        printf("Frog Distance = ");
        init();
        input();
        Prim(); 
    }       
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

poj 2253 Frogger(最小生成树)

标签:

原文地址:http://blog.csdn.net/llx523113241/article/details/47056241

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!