POJ 2253 Frogger

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.
題意:給n個點,求出第一個點到第二個點的多條路徑中任意兩點最長距離的最短的距離。
可以看做dijkstra的變形,貪心思想,用數組d[i]代表i到源點路徑中相鄰兩點的最長距離。

#include<iostream>
#include<stdio.h>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
#include<cmath>
#include<functional>
using namespace std;
#define N 1000+5
#define MAXN 1000000
#define mem(arr,a) memset(arr,a,sizeof(arr))
#define INF 0x3f3f3f3f
#define LL long long int 
#define pow(a) (a)*(a)
double d[N];
struct node{
    int x, y;
}p[N];
int vis[N];
double cost[N][N];
int n;
int cnt = 1;
double dis(node a, node b){
    return sqrt((double)pow(a.x - b.x) + (double)pow(a.y - b.y));
}
void dijkstra(){
    mem(d, 0);
    mem(vis, 0);

    while (1){
        int v = -1;
        for (int i = 1; i <= n; i++){
            if (!vis[i] && (v == -1 || d[v] > d[i]))v = i;
        }
        if (v == -1)break;
        vis[v] = 1;
        for (int i = 1; i <= n; i++){
            if (!vis[i])
            {
                if (!d[i])d[i] = max(d[v], cost[v][i]);
                else if (d[i] < d[v])continue;
                else d[i] = min(d[i], max(d[v], cost[v][i]));
            }
        }
    }
    printf("Scenario #%d\nFrog Distance = %.3f\n\n", cnt++, d[2]);
}
int main(){
    while (cin >> n){
        if (n == 0)break;
        for (int i = 1; i <= n; i++){
            cin >> p[i].x >> p[i].y;
        }
        mem(cost, 0);
        for (int i = 1; i <= n; i++){
            for (int j = 1; j <= n; j++){
                cost[i][j] = dis(p[i], p[j]);
            }
        }
        dijkstra();
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章