NBUT 1579 小青蛙找妈妈 dijkstra,flody最短路
一个坐标系上有n个点,青蛙在第一个点,青蛙妈妈在第二个点,青蛙只能在n个点之间跳来跳去,青蛙最少需要多少弹跳能力才能跳到青蛙妈妈所在的点。
其实是将青蛙跳的路径上两点之间的最大距离最小化。最开始想的时候写的flody,交上去超时了,后来想到因为求的只是两点之间的,可以用dijkstra。
后来学长说用的flody,不用stl就过了。
代码:
/*************************************************************************
> File Name: c.cpp
> Author: gwq
> Mail: gwq5210@qq.com
> 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;
}