湖中有n块石头,编号从1到n,有两只青蛙,Bob在1号石头上,Alice在2号石头上,Bob想去看望Alice,但由于水很脏,他想避免游泳,于是跳着去找她。但是Alice的石头超出了他的跳跃范围。因此,Bob使用其他石头作为中间站,通过一系列的小跳跃到达她。两块石头之间的青蛙距离被定义为两块石头之间所有可能路径上的最小必要跳跃距离,某条路径的必要跳跃距离即这条路径中单次跳跃的最远跳跃距离。你的工作是计算Alice和Bob石头之间的青蛙距离。
Input
多实例输入
先输入一个整数n表示石头数量,当n等于0时结束。
接下来2-n+1行依次给出编号为1到n的石头的坐标xi , yi。
2 <= n <= 200
0 <= xi , yi <= 1000
Output
先输出"Scenario #x", x代表样例序号。
接下来一行输出"Frog Distance = y", y代表你得到的答案。
每个样例后输出一个空行。
(ps:wa有可能是精度问题,g++不对可以用c++尝试,都不对就是代码问题)
Sample Input
2 0 0 3 4 3 17 4 19 4 18 5 0
Sample Output
Scenario #1 Frog Distance = 5.000 Scenario #2 Frog Distance = 1.414
题解:最短路的松弛操作变形一下就可以了,dis[i]表示从1到i的路径中最长的一段。其余都是SPFA模板。
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <queue>
#include <math.h>
using namespace std;
const int maxn = 50000;
int head[maxn],inque[maxn],tot;
float dis[maxn];
struct node
{
float x,y;
}nod[maxn];
struct edge
{
int v;
int next;
float len;
}edg[maxn];
void addnode(int u,int v,float len)
{
edg[tot].v = v;
edg[tot].len = len;
edg[tot].next = head[u];
head[u] = tot++;
}
void SPFA()
{
queue<int>Q;
Q.push(1);
inque[1] = 1;
dis[1] = 0;
while(!Q.empty())
{
int u = Q.front();
Q.pop();
inque[u] = 0;
for(int i = head[u];i != - 1;i = edg[i].next)
{
int v = edg[i].v;
float len = edg[i].len;
if(dis[v] > max(dis[u],len))//如果1到v的最大短比1到u或者u,v间的最大段大,则更新
{
dis[v] = max(dis[u],len);
if(!inque[v])
{
Q.push(v);
inque[v] = 1;
}
}
}
}
}
int main()
{
int n;
int cas = 0;
while(scanf("%d",&n) && n)
{
tot = 0;
for(int i = 0;i <= n;i++) dis[i] = 20000;
memset(head,-1,sizeof(head));
memset(inque,0,sizeof(inque));
for(int i = 1;i <= n;i++)
{
scanf("%f %f",&nod[i].x,&nod[i].y);
}
for(int i = 1;i <= n;i++)
{
for(int j = i + 1;j <= n;j++)
{
float len = sqrt((nod[i].x - nod[j].x) * (nod[i].x - nod[j].x) + (nod[i].y - nod[j].y) * (nod[i].y - nod[j].y));
addnode(i,j,len);
addnode(j,i,len);
}
}
SPFA();
printf("Scenario #%d\n",++cas);
printf("Frog Distance = %.3f\n\n",dis[2]);
}
return 0;
}