POJ_2253_Frogger_最短路

队友不来训练真的好无聊啊,无法愉快玩耍。


题意:

水面上有一些已知坐标的石头,已知屌丝所在石头的坐标和女神所在石头的坐标,屌丝想通过在石头上跳跃到达女神那块石头,求屌丝最少要能跳多远。



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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值