一.原题链接:http://poj.org/problem?id=2253
二.题目大意:青蛙要从0 号石头跳到1号石头,之间可以借助任意石头跳到1号,求在此过程中跳的每条总的路径中(从0到1)单次跳最大值 的最小值。
三.思路:一开始想不出,后来回去看了下Floyd,发现这个算法其实可以变形,它原本的递推公式是dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]),而这道题只要把其改为
dp[i][j] = min(dp[i][j], max(dp[i][k], dp[k][j]))就行了。
四代码:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
const int MAX_SIZE = 202,
INF = 1<<30,
MOD = 1000000007;
struct Point
{
double x, y;
};
Point pos[MAX_SIZE];
double graph[MAX_SIZE][MAX_SIZE];
int nodeNum;
double Floyd()
{
int i, j, k;
for(k = 0; k < nodeNum; k++)
for(i = 0; i < nodeNum; i++)
for(j = 0; j < nodeNum; j++)
graph[i][j] = min(graph[i][j], max(graph[i][k], graph[k][j]));
return graph[0][1];
}
int main()
{
//freopen("in.txt", "r", stdin);
int i, j, x, y, test = 1;
while(cin>>nodeNum && nodeNum){
memset(graph, 0, sizeof(graph));
for(i = 0; i < nodeNum; i++)
cin>>pos[i].x>>pos[i].y;
for(i = 0; i < nodeNum; i++)
for(j = i; j < nodeNum; j++)
graph[i][j] = graph[j][i] =
sqrt( (pos[i].x - pos[j].x)*(pos[i].x - pos[j].x) +
(pos[i].y - pos[j].y)*(pos[i].y - pos[j].y) );
printf("Scenario #%d\nFrog Distance = %.3f\n\n", test++, Floyd());
}
}