POJ 3037:Skiing(SPFA)

http://poj.org/problem?id=3037

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的变化v=v*2^(移动前坡度-移动后坡度),求从左上角到右下角最短时间是多少。

解题思路:

SPFA跑相邻的四个方向,遇到时间更短的加入队列。INF要取大一点,不然很容易WA,

最坑的还是精度问题!!POJ用%f输出,不然会WA

#include <stdio.h>
#include <string.h>
#include <queue>
#include <math.h>
#define N 120
using namespace std;
int a[N][N];
bool book[N][N];
double time[N][N]; 
int n, m, v;
double inf=9223372036854775800000000000;
                    
struct date{
	int x;
	int y;
	double v;
};
void spfa()
{
	int nex[4][2]={0,1, 0,-1, 1,0, -1,0};
	int tx, ty, i;
	memset(book, false, sizeof(book));
	for(i=1; i<=n; i++)
	{
		for(int j=1; j<=m; j++)
			time[i][j]=inf;
	}
	time[1][1]=0;
	queue<date>q;
	q.push(date{1, 1, v});
	book[1][1]=true;
	while(!q.empty())
	{
		date u=q.front();
		q.pop();
		book[u.x][u.y]=false;
		for(i=0; i<4; i++)
		{
			tx=u.x+nex[i][0];
			ty=u.y+nex[i][1];
			if(tx<=0 || ty<=0 || tx>n || ty>m)
				continue;
			if(time[tx][ty]>time[u.x][u.y]+1.0/u.v)
			{
				time[tx][ty]=time[u.x][u.y]+1.0/u.v;
				if(book[tx][ty]==false)
				{
					book[tx][ty]=true;
					q.push(date{tx, ty, u.v * pow(2, a[u.x][u.y]-a[tx][ty])});
					
				}
			}
		}
	}
}
int main()
{
	int i, j;
	while(scanf("%d%d%d", &v, &n, &m)!=EOF)
	{
		for(i=1; i<=n; i++)
			for(j=1; j<=m; j++)
				scanf("%d", &a[i][j]);
		spfa();
		printf("%.2f\n", time[n][m]);
	}
	return 0;
}

 

posted @ 2019-07-27 10:29  宿星  阅读(102)  评论(0编辑  收藏  举报