NBUT 1579 小青蛙找媽媽 dijkstra,flody最短路

NBUT 1579 小青蛙找媽媽 dijkstra,flody最短路

一個座標系上有n個點,青蛙在第一個點,青蛙媽媽在第二個點,青蛙只能在n個點之間跳來跳去,青蛙最少需要多少彈跳能力才能跳到青蛙媽媽所在的點。

其實是將青蛙跳的路徑上兩點之間的最大距離最小化。最開始想的時候寫的flody,交上去超時了,後來想到因爲求的只是兩點之間的,可以用dijkstra。

後來學長說用的flody,不用stl就過了。

代碼:

/*************************************************************************
    > File Name: c.cpp
    > Author: gwq
    > Mail: [email protected] 
    > Created Time: 2015年05月01日 星期五 15時27分02秒
 ************************************************************************/

#include <cmath>
#include <ctime>
#include <cctype>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <algorithm>

#define INF (INT_MAX / 10)
#define clr(arr, val) memset(arr, val, sizeof(arr))
#define pb push_back
#define sz(a) ((int)(a).size())

using namespace std;
typedef set<int> si;
typedef vector<int> vi;
typedef map<int, int> mii;
typedef long long ll;

const double esp = 1e-5;

#define N 2100

struct Point {
    double x, y;
    Point() {}
    Point(int xx, int yy)
    {
        x = xx;
        y = yy;
    }
    Point(double xx, double yy)
    {
        x = xx;
        y = yy;
    }
    int input(void)
    {
        return scanf("%lf%lf", &x, &y);
    }
}pt[N];

int vis[N];
double dp[N], fdp[N][N];

double Max(double a, double b)
{
    return a > b ? a : b;
}

double Min(double a, double b)
{
    return a < b ? a : b;
}

double dis(Point u, Point v)
{
    return sqrt(pow(u.x - v.x, 2.0) + pow(u.y - v.y, 2.0));
}

void floyd(int n)
{
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            fdp[i][j] = dis(pt[i], pt[j]);
        }
    }
    for (int k = 0; k < n; ++k) {
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                //dp[i][j] = min(dp[i][j], max(dp[i][k], dp[k][j]));
                fdp[i][j] = Min(fdp[i][j], Max(fdp[i][k], fdp[k][j]));
            }
        }
    }
}


void dijkstra(int n)
{
    clr(vis, 0);
    dp[0] = 0.0;
    for (int i = 1; i < n; ++i) {
        dp[i] = INF;
    }
    for (int i = 0; i < n; ++i) {
        int x;
        double m = INF + 5;
        for (int j = 0; j < n; ++j) {
            if (!vis[j] && dp[j] <= m) {
                x = j;
                m = dp[j];
            }
        }
        vis[x] = 1;
        for (int j = 0; j < n; ++j) {
            dp[j] = min(dp[j], max(dp[x], dis(pt[x], pt[j])));
        }
    }
}

int main(int argc, char *argv[])
{
    int n;
    int c = 0;
    while (scanf("%d", &n) != EOF) {
        if (n == 0) {
            break;
        }
        for (int i = 0; i < n; ++i) {
            pt[i].input();
        }
        //floyd(n);
        dijkstra(n);
        printf("Case #%d\n", ++c);
        printf("Distance = %.3f\n\n", dp[1]);
    }

    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章