POJ - 2253 Frogger 单源最短路

时间:2023-03-09 03:17:42
POJ - 2253 Frogger 单源最短路

题意:给定n个点的坐标,问从第一个点到第二个点的最小跳跃范围。d(i)表示从第一个点到达第i个点的最小跳跃范围.

AC代码

#include <cstdio>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <cstring>
#include <utility>
#include <string>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define eps 1e-10
#define inf 0x3f3f3f3f
#define PI pair<int, int>
typedef long long LL;
const int maxn = 200 + 5;
int d[maxn], vis[maxn];

struct node{
	int x, y;
}a[maxn];

struct HeapNode{
	int d, u;
	HeapNode() {}
	HeapNode(int d, int u):d(d), u(u) {}
	bool operator < (const HeapNode &p) const {
		return d > p.d;
	}
};

void dijkstra(int s, int n) {
	priority_queue<HeapNode>q;
	memset(d, inf, sizeof(d));
	d[0] = 0;
	memset(vis, 0, sizeof(vis));
	q.push(HeapNode(0, 0));
	while(!q.empty()) {
		HeapNode p = q.top(); q.pop();
		int u = p.u;
		if(vis[u]) continue;
		vis[u] = 1;
		for(int i = 0; i < n; ++i) {
			if(u == i) continue;
			int dis = (a[u].x - a[i].x)*(a[u].x - a[i].x) + (a[u].y - a[i].y)*(a[u].y - a[i].y);
			dis = max(dis, d[u]);
			if(d[i] > dis) {
				d[i] = dis;
				q.push(HeapNode(d[i], i));
			}
		}
	}
}

int main() {
	int n, kase = 1;
	while(scanf("%d", &n) == 1 && n) {
		for(int i = 0; i < n; ++i) scanf("%d%d", &a[i].x, &a[i].y);
		dijkstra(0, n);
		printf("Scenario #%d\n", kase++);
		printf("Frog Distance = %.3f\n\n", sqrt((double)d[1]));
	}
	return 0;
}

如有不当之处欢迎指出!