POJ 2253 Frogger
题目链接:POJ 2253 Frogger
题目大意:
湖中有\(n\)块石头,编号从\(1\)到\(n\),有两只青蛙,Bob在\(1\)号石头上,Alice在\(2\)号石头上,Bob想去看望Alice,但由于水很脏,他想避免游泳,于是跳着去找她。但是Alice的石头超出了他的跳跃范围。因此,Bob使用其他石头作为中间站,通过一系列的小跳跃到达她。两块石头之间的青蛙距离被定义为两块石头之间所有可能路径上的最小必要跳跃距离,某条路径的必要跳跃距离即这条路径中单次跳跃的最远跳跃距离。你的工作是计算Alice和Bob石头之间的青蛙距离。
题解:
对最短路稍加修改,把求最短路改成求局部最大和整体最小。
由于数据量较小,所以Floyd也可以过。
Floyd
#include <iostream>
#include <cmath>
#include <iomanip>
#include <cstring>
using namespace std;
#define ms(a, b) memset(a, b, sizeof(a))
#define N 300
#define INF 0x3f3f3f3f
#define io_speed_up ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
int x[N], y[N], n;
double dis[N][N];
void floyd() {
for (int k = 1; k <= n; ++k) {
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
dis[i][j] = min(dis[i][j], max(dis[i][k], dis[k][j]));
}
}
}
}
int main() {
io_speed_up;
cout << fixed << setprecision(3);
int t = 0;
while (cin >> n && n) {
ms(dis, 0);
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) {
dis[i][j] = dis[j][i] = sqrt(double((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j])));
}
}
floyd();
cout << "Scenario #" << ++t << endl << "Frog Distance = " << dis[1][2] << endl << endl;
}
return 0;
}
Dijkstra
#include <iostream>
#include <cstring>
#include <cmath>
#include <iomanip>
using namespace std;
#define ms(a, b) memset(a, b, sizeof(a))
#define N 300
#define INF 0x3f3f3f3f
#define io_speed_up ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
int x[N], y[N], n;
double mat[N][N], dis[N];
bool vis[N];
void dijkstra() {
ms(vis, 0);
for (int i = 1; i <= n; ++i) {
dis[i] = INF;
}
dis[1] = 0;
for (int i = 1; i <= n; ++i) {
int minn = INF, temp;
for (int j = 1; j <= n; ++j) {
if (!vis[j] && dis[j] < minn) {
temp = j;
minn = dis[j];
}
}
vis[temp] = true;
for (int j = 1; j <= n; ++j) {
dis[j] = min(dis[j], max(dis[temp], mat[temp][j]));
}
}
}
int main() {
io_speed_up;
cout << fixed << setprecision(3);
int t = 0;
while (cin >> n && n) {
ms(mat, 0);
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) {
mat[i][j] = mat[j][i] = sqrt(double((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j])));
}
}
dijkstra();
cout << "Scenario #" << ++t << endl << "Frog Distance = " << dis[2] << endl << endl;
}
return 0;
}
SPFA
#include <iostream>
#include <cstring>
#include <cmath>
#include <queue>
#include <iomanip>
using namespace std;
#define ms(a, b) memset(a, b, sizeof(a))
#define N 300
#define INF 0x3f3f3f3f
#define io_speed_up ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
int x[N], y[N], n;
double mat[N][N], dis[N];
bool vis[N];
void spfa() {
queue <int> q;
ms(vis, 0);
for (int i = 1; i <= n; ++i) {
dis[i] = INF;
}
dis[1] = 0;
vis[1] = true;
q.push(1);
while (!q.empty()) {
int temp = q.front();
vis[temp] = false;
q.pop();
for (int i = 1; i <= n; ++i) {
if (max(dis[temp], mat[temp][i]) < dis[i]) {
dis[i] = max(dis[temp], mat[temp][i]);
if (!vis[i]) {
q.push(i);
vis[i] = true;
}
}
}
}
}
int main() {
io_speed_up;
cout << fixed << setprecision(3);
int t = 0;
while (cin >> n && n) {
ms(mat, 0);
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) {
mat[i][j] = mat[j][i] = sqrt(double((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j])));
}
}
spfa();
cout << "Scenario #" << ++t << endl << "Frog Distance = " << dis[2] << endl << endl;
}
return 0;
}