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;
}

如有不当之处欢迎指出!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值