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 la
题意比较难懂,难点也在于转换题意
题意:找a-b所有路径中最大步数里面最小的
本来先用最小生成树写的,但是没写出来:
#include
#include
#include
#include
#define inf 0x3f3f3f3f
using namespace std;
//kruskal
int n,p;
struct edge
{
double x;
double y;
}e[];
double addedge[];
int f[];
double pre[];
double cmp1(double x,double y)
{
return x<y;
}
double d(double x1,double y1,double x2,double y2)
{
return sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
}
void init()
{
for(int i=;i<p;i++)
f[i]=i;
return ;
}
int getf(int x)
{
if(f[x]==x)
return x;
return getf[f[x]];
}
int merge(int x,int y)
{
int t1=merge(x);
int t2=merge(y);
if(t1!=t2)
{
f[t2]=t1;
return ;
}
return ;
}
int main()
{
std::ios::sync_with_stdio(false);
cin.tie();
cout.tie();
while(cin>>n)
{
if(n==)
break;
memset(e,,sizeof(e));
memset(addedge,,sizeof(addedge));
memset(f,,sizeof(f));
for(int i=;i<n;i++)
cout<<e\[i\].x<<e\[i\].y;
p=;//p条边
for(int i=;i<n;i++)
{
for(int j=i+;j<n;j++)
{
addedge\[p++\]=d(e\[i\].x,e\[i\].y,e\[j\].x,e\[j\].y);
}
}
sort(addedge+,addedge+p+,cmp1);
init();
int minmaxx=-inf;
int countt=;
int q=;
for(int i=;i<p;i++)
{
for(int j=i+;j<p;j++)
{
if(merge(addedge\[i\],addedge\[j\])==)//说明还未连通
{
countt++;
pre\[q++\]=
minmaxx=max(minmaxx,)
}
if(countt==p-)
break;
}
}
int tt=;
cout<<"Scenario #"<<tt++<<endl;
cout<<"Frog Distance = ";
cout<<setiosflags(ios::fixed)<<setprecision()<<dis<<endl;
}
return ;
}
AC的是用那个五行代码过的:
#include
#include
#include
#include
#define inf 0x3f3f3f3f
using namespace std;
//点点之间的距离floyd
//找a-b所有路径中最大步数里面最小的
struct edge
{
// double x;
// double y;
int x;
int y;
} e[];
double a[][];
int n;
double d(int x1,int y1,int x2,int y2)
{
return sqrt(((x2-x1)*(x2-x1)*1.0+(y2-y1)*(y2-y1)*1.0)*1.0);
}
void init()
{
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
if(i==j)
a[i][j]=;
else
a[i][j]=inf;
}
}
}
int main()
{
std::ios::sync_with_stdio(false);
cin.tie();
cout.tie();
int tt=;
while(cin>>n)
{
if(n==)
break;
memset(e,,sizeof(e));
memset(a,,sizeof(a));
init();
for(int i=; i<=n; i++)//n个顶点
cin>>e[i].x>>e[i].y;
// p=1;//p条边
for(int i=; i<=n; i++)
{
for(int j=i+; j<=n; j++)
{
// addedge\[p++\]=d(e\[i\].x,e\[i\].y,e\[j\].x,e\[j\].y);
double dd=d(e\[i\].x,e\[i\].y,e\[j\].x,e\[j\].y);
if(a\[i\]\[j\]>dd||a\[j\]\[i\]>dd)
{
a\[j\]\[i\]=a\[i\]\[j\]=dd;
}
}
}
// sort(addedge+1,addedge+p+1,cmp1);
for(int k=;k<=n;k++)
{
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
//if(a\[i\]\[j\]a\[i\]\[k\]+a\[k\]\[j\])
a\[i\]\[j\]=min(a\[i\]\[j\],max(a\[i\]\[k\],a\[k\]\[j\]));
}
}
}
double dis=a\[\]\[\];
cout<<"Scenario #"<<tt++<<endl;
cout<<"Frog Distance = ";
cout<<setiosflags(ios::fixed)<<setprecision()<<dis<<endl<<endl;
}
return ;
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章