POJ-3037-Skiing(最短路)

101 阅读1分钟

                                                             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

题意描述:

求从1,1点到R,C点的最短时间,给你了1,1点的速度,他的速度都只与初始速度,初始高度,现在高度有关,所以不必随时更新速度,这和重力势能和动能转化的常识是一致的v[i][j]=V*pow( 2.0,1.0*(h[1][1]-h[i][j]));时间是其倒数,需要注意的是初始化inf时要初始化为0x7f7f7f7f,我之前初始化为99999999就没过,还有输出时要%f,我之前用%lf也没过

程序代码:

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
#include<queue>
#define inf 0x7f7f7f7f
struct data{
	int x;
	int y;
};
int next[4][2]={0,1, 1,0, -1,0, 0,-1};
int h[110][110],book[110][110];
double v[110][110],dis[110][110];
int main()
{
	int i,j,R,C;
	double w,V;
	struct data A,B;
	queue<data>que;
	while(scanf("%lf%d%d",&V,&R,&C)!=EOF)
	{
		memset(book,0,sizeof(book));
		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(!que.empty())
		{
			que.pop();
		}
		A.x=1;A.y=1;
		que.push(A);
		book[1][1]=1;
		dis[1][1]=0;
		while(!que.empty())
		{
			A=que.front();
			que.pop();
			book[A.x][A.y]=0;
			for(i=0;i<4;i++)
			{
				B.x=A.x+next[i][0];
				B.y=A.y+next[i][1];
				if(B.x<1||B.x>R||B.y<1||B.y>C)
					continue;
				w=1.0/v[A.x][A.y];
				if(dis[B.x][B.y]>dis[A.x][A.y]+w)
				{
					dis[B.x][B.y]=dis[A.x][A.y]+w;
					if(book[B.x][B.y]==0)
					{
						que.push(B);
						book[B.x][B.y]=1;	
					}	
				}
			}
		}
		printf("%.2f\n",dis[R][C]); 
	}
	return 0;
}