Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fiona Frog who is sitting on another stone. He plans to visit her, but since the water is dirty and full of tourists' sunscreen, he wants to avoid swimming and instead reach her by jumping.
Unfortunately Fiona's stone is out of his jump range. Therefore Freddy considers to use other stones as intermediate stops and reach her by a sequence of several small jumps.
To execute a given sequence of jumps, a frog's jump range obviously must be at least as long as the longest jump occuring in the sequence.
The frog distance (humans also call it minimax distance) between two stones therefore is defined as the minimum necessary jump range over all possible paths between the two stones.
You are given the coordinates of Freddy's stone, Fiona's stone and all other stones in the lake. Your job is to compute the frog distance between Freddy's and Fiona's stone.
Input
The input will contain one or more test cases. The first line of each test case will contain the number of stones n (2<=n<=200). The next n lines each contain two integers xi,yi (0 <= xi,yi <= 1000) representing the coordinates of stone #i. Stone #1 is Freddy's stone, stone #2 is Fiona's stone, the other n-2 stones are unoccupied. There's a blank line following each test case. Input is terminated by a value of zero (0) for n.
Output
For each test case, print a line saying "Scenario #x" and a line saying "Frog Distance = y" where x is replaced by the test case number (they are numbered from 1) and y is replaced by the appropriate real number, printed to three decimals. Put a blank line after each test case, even after the last one.
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][j]=min(dis[i][j],max(dis[i][k]+dis[k][j]))
数据比较小,下面用的floyd。dijk也是一样。
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;
const int N=250;
const int inf=0x3f3f3f3f;
double g[N][N];
int x[N],y[N];
int n;
void floyd()
{
for(int k=1;k<=n;k++)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
g[i][j]=min(g[i][j],max(g[i][k],g[k][j]));///修改
}
}
}
int main()
{
int kcase=0;
while(scanf("%d",&n)!=EOF&&n)
{
memset(x,0,sizeof(x));
memset(y,0,sizeof(y));
memset(g,0,sizeof(g));
scanf("%d%d",&x[1],&y[1]);
scanf("%d%d",&x[n],&y[n]);
for(int i=2;i<n;i++)
scanf("%d%d",&x[i],&y[i]);
getchar();
for(int i=1;i<=n;i++)
{
g[i][i]=0;
for(int j=i+1;j<=n;j++)
{
g[i][j]=g[j][i]=1.0*sqrt(1.0*(x[i]-x[j])*(x[i]-x[j])+1.0*(y[i]-y[j])*(y[i]-y[j]));
}
}
floyd();
printf("Scenario #%d\nFrog Distance = %.3f\n\n",++kcase,g[1][n]);
}
return 0;
}
dijkstra:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
const int N = 250;
const double inf = 0x3f3f3f3f;
double g[N][N], dis[N];
bool vis[N];
int n;
struct node
{
double x, y;
}s[N];
void dijk()
{
memset(vis, 0, sizeof(vis));
for(int i = 1; i <= n; i++)
dis[i] = g[1][i];
vis[1] = 1;
dis[1] = 0;
for(int i = 1; i <= n; i++)
{
int pos, minn = inf;
for(int j = 1; j <= n; j++)
{
if(!vis[j] && minn > dis[j])
{
pos = j;
minn = dis[j];
}
}
vis[pos] = 1;
for(int j = 1; j <= n; j++)
{
if(!vis[j] && dis[j] > max(dis[pos], g[pos][j]))
{
dis[j] = max(dis[pos], g[pos][j]);
}
}
}
}
int main()
{
double a, b;
int kcase = 0;
while(scanf("%d", &n) != EOF && n)
{
memset(s, 0, sizeof(s));
for(int i = 1; i <= n; ++i)
{
scanf("%lf%lf", &a, &b);
if(i == 1)
{
s[i].x = a;
s[i].y = b;
}
else if(i == 2)
{
s[n].x = a;
s[n].y = b;
}
else
{
s[i - 1].x = a;
s[i - 1].y = b;
}
}
for(int i = 1; i <= n; ++i)
{
for(int j = i; j <= n; ++j)
{
g[i][j] = g[j][i] = 1.0 * sqrt((s[i].x - s[j].x) * (s[i].x - s[j].x) + (s[i].y - s[j].y) * (s[i].y - s[j].y));
}
}
dijk();
cout<<"Scenario #"<<++kcase<<'\n';
printf("Frog Distance = %.3f\n\n", dis[n]);
}
return 0;
}
用最小生成树做:
从最短边开始生成树,在两点联通的同时,结束生成树。
得到的就是这个不完整的生成树中的所有路径中的最大值的最小值(有点绕
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;
const int N=45000;///边数组开大点
const int inf=0x3f3f3f3f;
struct node
{
int u,v;
double w;
bool operator <(const node &a)const
{
return w<a.w;
}
}edge[N];
int father[250];
int n,m;
int x[250],y[250];
double ans;
void init()
{
for(int i=0;i<=n;i++)
father[i]=i;
}
int Find(int x)
{
if(x==father[x])
return x;
return father[x]=Find(father[x]);
}
void Union(int x,int y)
{
int tmpx=Find(x);
int tmpy=Find(y);
if(tmpx!=tmpy)
{
father[tmpx]=tmpy;
}
}
double kruskal()
{
sort(edge,edge+m);
init();
node now;
double ans=0;
for(int i=0;i<m;i++)
{
now=edge[i];
if(Find(now.u)!=Find(now.v))
{
Union(now.u,now.v);
if(Find(1)==Find(n))
return now.w;
}
}
}
int main()
{
int kcase=0;
while(scanf("%d",&n)!=EOF&&n)
{
memset(x,0,sizeof(x));
memset(y,0,sizeof(y));
scanf("%d%d",&x[1],&y[1]);
scanf("%d%d",&x[n],&y[n]);
for(int i=2;i<n;i++)
scanf("%d%d",&x[i],&y[i]);
getchar();
m=0;
for(int i=1;i<=n;i++)
{
for(int j=i+1;j<=n;j++)
{
edge[m].u=i;
edge[m].v=j;
edge[m].w=1.0*sqrt(1.0*(x[i]-x[j])*(x[i]-x[j])+1.0*(y[i]-y[j])*(y[i]-y[j]));
m++;
}
}
ans=kruskal();
printf("Scenario #%d\nFrog Distance = %.3f\n\n",++kcase,ans);
}
return 0;
}