欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

POJ【2253】Frogger(最小最大值)

程序员文章站 2024-03-15 22:00:24
...

题目链接:http://poj.org/problem?id=2253

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath>
using namespace std;
#define INF 0x3f3f3f3f
const int N = 200 + 10;
int n;
double a[N][N],d[N],inq[N];
struct Stone {
	int x,y;
} s[N];
void SPFA(int s)
{
	for(int i = 0; i < n; i++) {
		d[i] = INF;
		inq[i] = 0;
	}
	d[s] = 0;//从起点到终点的路径中某一权值最小的边
	queue<int> q;
	q.push(s);
	while(!q.empty()) {
		int u = q.front();q.pop();
		inq[u] = 0;
		for(int v = 0; v < n; v++)
			if(d[v] > max(d[u],a[u][v])) {//修改松弛操作
				//d[v] = min(d[v],max(d[u],a[u][v]));
				d[v] = max(d[u],a[u][v]);
				if(!inq[v]) {
					inq[v] = 1;
					q.push(v);
				}
			}
	}
}
double calc(Stone s1,Stone s2)
{
	return sqrt(1.0 * (s1.x - s2.x) * (s1.x - s2.x) + 1.0 * (s1.y - s2.y) * (s1.y - s2.y));
}
int main()
{
	int k = 0;
	while(scanf("%d",&n) && n) {
		for(int i = 0; i < n; i++)
			for(int j = 0; j < n; j++)
				a[i][j] = INF;
		for(int i = 0; i < n; i++)
			scanf("%d%d",&s[i].x,&s[i].y);
		for(int i = 0; i < n; i++)
			for(int j = i + 1; j < n; j++)
				a[i][j] = a[j][i] = calc(s[i],s[j]);
		SPFA(0);
		if(k++) printf("\n");
		printf("Scenario #%d\n",k);
		printf("Frog Distance = %.3f\n",d[1]);//注意printf不要使用lf
	}
	return 0;
}
相关标签: 最小最大值