poj2253
dijkstra
把存最短距离的数组改为存储frog distance即可
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> using namespace std; const int maxn = 201, inf = 1000000000; struct point { int x, y; } stone[maxn]; int n; double dist[maxn]; bool vis[maxn]; void init() { for (int i = 0; i < n; i++) scanf("%d%d", &stone[i].x, &stone[i].y); } double distan(point &a, point &b) { return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); } void dijkstra() { double best = 0; int besti = 0; for (int i = 0; i < n; i++) dist[i] = inf; memset(vis, 0, sizeof(vis)); vis[0] = true; dist[0] = 0; while (besti != -1 && !vis[1]) { for (int i = 0; i < n; i++) { double temp = max(dist[besti], distan(stone[i], stone[besti])); if (dist[i] > temp) dist[i] = temp; } best = inf; besti = -1; for (int i = 0; i < n; i++) if (!vis[i] && dist[i] < best) { best = dist[i]; besti = i; } vis[besti] = true; } } int main() { //freopen("D:\\t.txt", "r", stdin); int t = 0; while (scanf("%d", &n) != EOF && n != 0) { init(); dijkstra(); t++; printf("Scenario #%d\nFrog Distance = %.3f\n\n", t, dist[1]); } return 0; }