队友不来训练真的好无聊啊,无法愉快玩耍。
题意:
水面上有一些已知坐标的石头,已知屌丝所在石头的坐标和女神所在石头的坐标,屌丝想通过在石头上跳跃到达女神那块石头,求屌丝最少要能跳多远。
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.
一开始脑残地写了神搜,还没交就发现200!简直要死。
然后跑去吃饭了,回来的路上想明白了这个题要用最短路算法,虽然求得又不是最短路,但是应该是所有的最短路算法都可以,把最短路里面记录到达当前点经历的长度的量改成记录到达当前点经历的最跳跃就行了,像我这么朴素的人,果断写了dijkstra的朴素实现。
做了这几个用最短路算法不求最短路的题以后,深深的体会到学算法最重要的不是记住该怎么写,而是知道它为什么是对的,为什么要这样做,才能自己扩展出不同的算法。
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
using namespace std;
#define mxn 210
struct point{
double x,y;
}p[mxn];
double dist(point a,point b){
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double map[mxn][mxn];
int n;
double d[mxn];
bool flag[mxn];
void dijkstra(){
memset(flag,0,sizeof(flag));
for(int i=0;i<n;++i) d[i]=0.0+0x3f3f3f3f;
d[0]=0;
flag[0]=true;
int now=0;
for(int i=1;i<n;++i){
for(int j=0;j<n;++j){
if(j==now) continue;
d[j]=min(d[j],max(d[now],map[now][j]));
}
for(int j=0;j<n;++j)
if(!flag[j]&&(d[now]>d[j]||flag[now]))
now=j;
flag[now]=true;
}
}
int main(){
int cs=0;
while(scanf("%d",&n)!=EOF&&n){
for(int i=0;i<n;++i)
scanf("%f%f",&p[i].x,&p[i].y);
for(int i=0;i<n;++i)
for(int j=i;j<n;++j)
map[i][j]=map[j][i]=dist(p[i],p[j]);
dijkstra();
printf("Scenario #%d\n",++cs);
printf("Frog Distance = %.3f\n\n",d[1]);
}
return 0;
}