POJ - 3037 Skiing (spfa的bfs)

本文介绍了一种利用SPFA算法解决滑雪问题的方法。问题描述为:初始速度为v,在一个n*m的矩阵中给出了各个点的高度,从A到B的速度为A的速度*2^(hA-hB),到B的时间为A点速度的倒数。目标是从(1,1)点到(n,m)点的最小时间。通过SPFA算法找到最优路径。

                                                 Skiing

Description

Bessie and the rest of Farmer John's cows are taking a trip this winter to go skiing. One day Bessie finds herself at the top left corner of an R (1 <= R <= 100) by C (1 <= C <= 100) grid of elevations E (-25 <= E <= 25). In order to join FJ and the other cows at a discow party, she must get down to the bottom right corner as quickly as she can by travelling only north, south, east, and west.

Bessie starts out travelling at a initial speed V (1 <= V <= 1,000,000). She has discovered a remarkable relationship between her speed and her elevation change. When Bessie moves from a location of height A to an adjacent location of eight B, her speed is multiplied by the number 2^(A-B). The time it takes Bessie to travel from a location to an adjacent location is the reciprocal of her speed when she is at the first location.

Find the both smallest amount of time it will take Bessie to join her cow friends.

Input

* Line 1: Three space-separated integers: V, R, and C, which respectively represent Bessie's initial velocity and the number of rows and columns in the grid.

* Lines 2..R+1: C integers representing the elevation E of the corresponding location on the grid.

Output

A single number value, printed to two exactly decimal places: the minimum amount of time that Bessie can take to reach the bottom right corner of the grid.

Sample Input

1 3 3
1 5 3
6 3 5
2 4 3

Sample Output

29.00

Hint

Bessie's best route is:
Start at 1,1 time 0 speed 1
East to 1,2 time 1 speed 1/16
South to 2,2 time 17 speed 1/4
South to 3,2 time 21 speed 1/8
East to 3,3 time 29 speed 1/4

题意描述:
初始速度为v,在一个n*m的矩阵中给出了各个点的高度,从A到B的速度为A的速度*2^(hA-hB),到B的时间为A点速度的倒数。求从(1,1)点到(n,m)点的最小时间。

解题思路:

看了题解知道了a到c的速度为va*2^(a-b)*2^(b-c)=va*2^(a-c);所以可以求出各个点的速度;在用spfa的bfs算法求出到(n,m)的最小时间。还有就是这道题的无穷大inf不能定义为八个9要2147483647 不然会答案错误。

spfa算法中队首取消标记的作用:

SPFA中一个点可能在出队列之后再次被放入队列,也就是一个点改进过其它的点之后,过了一段时间可能本身被改进(重新入队),于是再次用来改进其它的点,这样反复迭代下去。

关于spfa算法:https://blog.youkuaiyun.com/xunalove/article/details/70045815

                         http://lib.youkuaiyun.com/article/datastructure/10344

#include<stdio.h>
#include<string.h>
#include<math.h>
# define inf 2147483647
int e[1010][1010],book[1010][1010];
double dis[1010][1010],map[1010][1010];//dis数组存储到各个点的时间,map数组存储各个点的速度 
int next[4][2]={0,1,
				1,0,
				0,-1,
				-1,0};
struct A
{
	int x;
	int y;
}q[10000010],t,f;
int main()
{
	int n,m,i,j,head,tail,k;
	double w,v;
	while(scanf("%lf%d%d",&v,&n,&m)!=EOF)
	{
		for(i=1;i<=n;i++)
			for(j=1;j<=m;j++)
				scanf("%d",&e[i][j]);
		for(i=1;i<=n;i++)
			for(j=1;j<=m;j++)
			{
				map[i][j]=(double)(v*(double)pow(2,1.0*(e[1][1]-e[i][j])));
			}
		map[1][1]=v;//存入各个点速度 
		memset(book,0,sizeof(book));
		for(i=1;i<=n;i++)
			for(j=1;j<=m;j++)
				dis[i][j]=inf;
		dis[1][1]=0;//初始化各个点的时间 
		book[1][1]=1;
		head=0;
		tail=1;
		t.x=1;
		t.y=1;
		q[head]=t;
		while(head<tail)
		{
			t=q[head];
			head++;
			book[t.x][t.y]=0;//队首取消标记 
			for(k=0;k<=3;k++)
			{
				f.x=t.x+next[k][0];
				f.y=t.y+next[k][1];
				w=1.0/map[t.x][t.y];
				if(f.x>=1&&f.x<=n&&f.y>=1&&f.y<=m&&dis[f.x][f.y]>dis[t.x][t.y]+w)
				{
					dis[f.x][f.y]=dis[t.x][t.y]+w;//符合条件更新dis数组为最小 
					if(book[f.x][f.y]==0)
					{
						q[tail]=f;
						tail++;
						book[f.x][f.y]=1;
					}
				 } 
			}
		}
		printf("%.2f\n",dis[n][m]);
	}
	return 0;
}

 

内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值