【Skiing】【POJ - 3037】(dijkstra+堆优化)

版权声明:本人原创,未经许可,不得转载 https://blog.csdn.net/qq_42505741/article/details/83758798

题目:

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)点到(i,j)点的2^高度差,而速度是其倒数,需输出的啦~,先把节点存储成耗时的静态邻接表,然后利用dijkstra的堆优化进行(nlogn)的求解。

ac代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
#include<cmath>
#define eps 1e-8
#define maxn 111
#define inf 999999999999

using namespace std;

struct node{
	int x;
	int y;
	double dis;
	bool operator < (const node &a) const{
		return dis>a.dis;
	}
};
int dic[4][2]={0,1,1,0,-1,0,0,-1};
int at[maxn][maxn];
double mp[maxn][maxn];
double dis[maxn][maxn];
bool vis[maxn][maxn];
int v,r,c;
void init()
{
	for(int i=1;i<=r;i++)
		for(int j=1;j<=c;j++)
			dis[i][j]=inf;
	memset(vis,0,sizeof(vis));
}
double get_cost(int h)
{
	return v*1.0*pow(2*1.0,at[1][1]-h);
}
bool ok(int x,int y)
{
	if(x>0&&x<=r&&y>0&&y<=c)
		return true;
	else 
		return false;
}
void dijkstra()
{
	priority_queue<node> q;
	node now,next;
	now.x=1;
	now.y=1;
	now.dis=0;
	dis[1][1]=0;
	q.push(now);
	while(!q.empty())
	{
		now=q.top();
		q.pop();
		if(vis[now.x][now.y]) continue;
		vis[now.x][now.y]=true;
		if(now.x==r&&now.y==c)
			break;
		int x;int y;
		for(int i=0;i<4;i++)	
		{
			x=now.x+dic[i][0];
			y=now.y+dic[i][1];
			if(!ok(x,y)) continue;
			if(!vis[x][y]&&dis[x][y]>now.dis+1.0/mp[now.x][now.y])
			{
				dis[x][y]=now.dis+1.0/mp[now.x][now.y];
				next.x=x;
				next.y=y;
				next.dis=dis[x][y];
				q.push(next);
			}
		}
	}	
}

int main()
{	
	scanf("%d%d%d",&v,&r,&c);
	init();
	//printf("%lf\n",dis[1][1]);
	for(int i=1;i<=r;i++)
		for(int j=1;j<=c;j++)
			scanf("%d",&at[i][j]);
	for(int i=1;i<=r;i++)
		for(int j=1;j<=c;j++)
			mp[i][j]=get_cost(at[i][j]);
	dijkstra();
	printf("%.2f\n",dis[r][c]);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_42505741/article/details/83758798