J - Skiing(广搜 SPfa 队列)

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

题意概括  :

贝西的奶牛和约翰的奶牛要去滑雪,为了加入一个舞会,需要赶快到达右下角,只能往东南西北四个方向移动,移动的过程中速度只与起始时的高度和现在的高度有关,所用时间为前一个位置的速度的倒数,求到达右下角所用最少时间是多少。

解题思路  :

SPFA 和广搜,找到右下角的点,同时更新最小时间,使时间达到最少。

SPFA :

#include<stdio.h>
#include<string.h>
#include<queue>
#include<math.h>
#define inf 0x7f7f7f7f

using namespace std;

int V,R,C,book[1010][1010],h[1010][1010];
double v[1010][1010],dis[1010][1010];
int dir[4][2] = {0,1,1,0,0,-1,-1,0};

struct node
{
	int x,y;
}now,next;

int main()
{
	int i,j,k;
	
	queue<node>Q;
	
	while(~ scanf("%d %d %d",&V,&R,&C))
	{
		for(i = 1;i<=R;i ++)
		{
			for(j = 1;j<=C;j ++)
			{
				scanf("%d",&h[i][j]);
				v[i][j] = V*(pow(2.0,1.0*(h[1][1]-h[i][j])));
			}
		}
		v[1][1] = V;
		for(i = 1;i<=R;i ++)
		{
			for(j = 1;j<=C;j ++)
			{
				dis[i][j] = inf;
			}
		}
		while(!Q.empty())
		{
			Q.pop();
		}
		now.x = 1;
		now.y = 1;
		Q.push(now);
		book[1][1] = 0;
		dis[1][1] = 0;
		while(!Q.empty())
		{
			now = Q.front();
			Q.pop();
			
			book[now.x][now.y] = 0;
			
			for(i = 0;i<=3;i ++)
			{
				next.x = now.x + dir[i][0];
				next.y = now.y + dir[i][1];
				
				if(next.x<1||next.x>R||next.y<1||next.y>C)
				continue;
				
				double w = 1.0/v[now.x][now.y];
				
				if(dis[next.x][next.y] > dis[now.x][now.y] + w)
				{
					dis[next.x][next.y] = dis[now.x][now.y] + w;
					if(book[next.x][next.y] == 0)
					{
						Q.push(next);
						book[next.x][next.y] = 1;
					}
				}
			}
		}
		printf("%.2f\n",dis[R][C]);
	}
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值