poj2253Frogger(kruskal&dijkstra)

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.

思路:这道题可以用dijkstra也可以用kruskal,不过用dijkstra一开始一直过不了,然后用kruskal一发过了。

kruskal:先建边,把所有边都放到一个vector里进行排序,然后挨个把没有联通的边联通,直到1和2两条边联通时break,有点水,1发ac

#include<queue>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdio>
#define fi first
#define se second
#define INF 0x3f3f3f3f
#define ll long long
#define ld long double
#define mem(ar,num) memset(ar,num,sizeof(ar))
#define me(ar) memset(ar,0,sizeof(ar))
#define lowbit(x) (x&(-x))
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define lcm(a,b) ((a)*(b)/(__gcd((a),(b))))
#define mod 1000000007
#define MP make_pair
#define PI pair<int,int>
using namespace std;
const int N = 1e5 + 10;
int n, k = 1, x[210], y[210], d[N];
double dis[N];
struct node {
    double x, y, len;
};
vector<node> v;
double len(int a, int b, int c, int d) {
    return sqrt((c - a) * (c - a) * 1.0 + (d - b) * (d - b) * 1.0);
}
int cmp(node a, node b) {
    return a.len < b.len;
}
int F(int x) {
    return d[x] == x ? x : d[x] = F(d[x]);
}
int main() {
    while(cin >> n && n) {
        me(dis);
        v.clear();
        double ans = 0;
        for(int i = 0; i < N; i++)
            d[i] = i;
        for(int i = 1; i <= n; i++)
            cin >> x[i] >> y[i];
        for(int i = 1; i < n; i++)
            for(int j = i + 1; j <= n; j++) {
                double l = len(x[i], y[i], x[j], y[j]);
                v.push_back({i, j, l});
                v.push_back({j, i, l});
            }
        sort(v.begin(), v.end(), cmp);
        for(int i = 0; i < v.size(); i++) {
            int x = F(v[i].x), y = F(v[i].y);
            if(x != y) {
                d[x] = y;
                ans = max(ans, v[i].len);
                if(F(1)==F(2))break;
            }
        }
        cout << "Scenario #" << k++ << endl;
        printf("Frog Distance = %.3f\n\n", ans);
    }
    return 0;
}

dijkstra:明天睡醒再改改bug,看看能不能过。。。

居然是宏定义的错误。。。我一直定义MP 为make_pair(int,int)  ,改成double,int就过了。。。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <math.h>
#include <queue>
#include <vector>
#include <algorithm>
#define fi first
#define se second
#define INF 0x3f3f3f3f
#define ll long long
#define ld long double
#define mem(ar,num) memset(ar,num,sizeof(ar))
#define me(ar) memset(ar,0,sizeof(ar))
#define lowbit(x) (x&(-x))
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define lcm(a,b) ((a)*(b)/(__gcd((a),(b))))
#define mod 1000000007
#define MP make_pair
#define PI pair<double,int>
using namespace std;
const int N = 1e5 + 10;
int n, k = 1, x[210], y[210], h[N], num, v, vis[N];
double dis[N], w;
struct node {
    int v, net;
    double w;
} no[N << 1];
void add(int u, int v, double w) {
    no[num].v = v;
    no[num].w = w;
    no[num].net = h[u];
    h[u] = num++;
}
double len(int a, int b, int c, int d) {
    return sqrt((c - a) * (c - a) * 1.0 + (d - b) * (d - b) * 1.0);
}
priority_queue<PI, vector<PI>, greater<PI> > q;
void dijkstra(int x) {
    for(int i = 0; i <= 210; i++)
        dis[i] = 723123123;
    dis[x] = 0;
    q.push(MP(0, x));
    while(!q.empty()) {
        w = q.top().first;
        v = q.top().second;
        q.pop();
        vis[v] = 1;
        for(int i = h[v]; i != -1; i = no[i].net) {
            int j = no[i].v;
            if(!vis[j] && dis[j] > max(w, no[i].w)) {
                dis[j] = max(w, no[i].w);
                q.push(MP(dis[j], j));
            }
        }
    }
}
int main() {
    while(cin >> n && n) {
        mem(h, -1), me(vis), me(dis);
        for(int i = 1; i <= n; i++)
            cin >> x[i] >> y[i];
        for(int i = 1; i < n; i++)
            for(int j = i + 1; j <= n; j++) {
                double l = len(x[i], y[i], x[j], y[j]);
                add(i, j, l);
                add(j, i, l);
            }
        dijkstra(1);
        cout << "Scenario #" << k++ << endl;
        printf("Frog Distance = %.3f\n\n", dis[2]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Endeavor_G/article/details/88809448