【题目链接】
http://poj.org/problem?id=2253
题目意思
两只青蛙,一只想跳到另一只石子上,现在给定n个石子坐标(第一和第二为青蛙坐标)问青蛙最少跳多远才能不掉入水的情况到另一只青蛙石子上。
解题思路
用单源最短路完成,不过dis不在存储从起点到某点最短距离,而存储这条路劲中最大距离的一段。(这题输出%lf过不了要用%f坑死了,wa到o(╥﹏╥))
代码部分
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <queue>
#include <string>
#include <math.h>
using namespace std;
#define LL long long
#define inf 0x3f3f3f3
const int N = 205;
double dis[N]; ///存储最短路
int vis[N]; ///记录是否在队列
int n;
double w[N][N]; ///两点间的距离
struct poin
{
int x,y;
}po[N]; ///点
void init() ///初始化
{
for (int i = 0;i <= n; i++)
{
dis[i] = inf;
vis[i] = 0;
}
}
double Width(poin x,poin y) ///两点距离
{
double w;
w = sqrt((x.x-y.x)*(x.x-y.x)+(x.y-y.y)*(x.y-y.y));
return w;
}
void spfa()
{
queue<int>q;
q.push(1);
vis[1] = 1;
dis[1] = 0;
while (!q.empty())
{
int t = q.front();
q.pop();
for (int i = 1; i <= n; i++)
{
if (dis[i] > max(dis[t],w[t][i])) ///松弛
{
dis[i] = max(dis[t],w[t][i]);
if (!vis[i]) ///判断是否在队列
{
q.push(i);
vis[i] = 1;
}
}
}
vis[t] = 0;
}
}
int main()
{
int cas = 1;
while (scanf("%d",&n),n)
{
init();
for(int i =1; i <= n;i++)
{
double s;
scanf("%d %d",&po[i].x,&po[i].y);
for (int j= 1 ;j <= i; j++) ///处理坐标边距离
{
s = Width(po[i],po[j]);
w[i][j] = s;
w[j][i] = s;
}
}
spfa();
printf("Scenario #%d\n",cas++);
printf("Frog Distance = %.3f\n\n",dis[2]);
}
return 0;
}