题目大致意思就是1想见2,它可以通过石头来中转,让路径中每次跳的最大距离最小。输出极限距离。
AC代码:
#include<iostream>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
const int inf=0x3f3f3f3f;
const int N=1010;
double d[N],w[N][N];
bool vis[N];
int n;
struct node{
double x;
double y;
}now[N];
void dsj()
{
memset(d,0x3f,sizeof(d));
d[1]=0.0;
for(int i=2;i<=n;i++)
d[i]=w[1][i];
vis[1]=1;
for(int i=1;i<=n;i++)
{
int t=-1;
for(int j=1;j<=n;j++)
{
if(!vis[j]&&(t==-1||d[t]>d[j]))
t=j;
}
vis[t]=1;
for(int j=1;j<=n;j++)
{
if(d[j]>max(d[t],w[t][j])&&!vis[j]&&w[t][j]!=inf)
d[j]=max(d[t],w[t][j]);
// printf("%lf",d[2]);
}
}
}
int main()
{
int k=0;
while(scanf("%d",&n)!=EOF)
{
memset(vis,0,sizeof(vis));
memset(w,0x3f,sizeof(w));
if(n==0) break;
for(int i=1;i<=n;i++)
{
scanf("%lf%lf",&now[i].x,&now[i].y);
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
w[i][j]=w[j][i]=sqrt((now[i].x-now[j].x)*(now[i].x-now[j].x)+(now[i].y-now[j].y)*(now[i].y-now[j].y));
}
dsj();
printf("Scenario #%d\n",++k);
printf("Frog Distance = %.3lf\n\n",d[2]);
}
return 0;
}
flyod做法
#include<algorithm>
#include<cmath>
#include<stdio.h>
#include<cstring>
#include<iostream>
using namespace std;
const int N=1010;
double g[N][N];
int m,n;
double d[N];
bool st[N];
struct node{
int x;
int y;
}frog[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 k=0;
while(cin>>n,n)
{
memset(st,0,sizeof st);
memset(g,0,sizeof g);
for(int i=1;i<=n;i++)
{
int x,y;
cin>>x>>y;
frog[i]={
x,y};
}
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
g[j][i]=g[i][j]=sqrt(1.0*(frog[i].x-frog[j].x)*(frog[i].x-frog[j].x)+(frog[i].y-frog[j].y)*(frog[i].y-frog[j].y));
floyd();
printf("Scenario #%d\n",++k);
printf("Frog Distance = %.3f\n\n",g[1][2]);
}
return 0;
}