POJ 3037-Skiing-最短路-Dijkstra+优先队列优化

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 height 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

题目大意:

给定一个RXC的网格,每一个格子都有相应的高度,从高处运动到低处,速度会加快,从低处运动到高处,速度会减慢。若速度为V时从A出运动至B处,则到达B处后速度变为V· 2^(A-B)。运动方向有上、下、左、右四个,相邻格子距离为1。求从网格左上角(1,1)运动至右下角(R,C)所需要的最短时间。

核心思想:

Dijkstra+优先队列优化。
dis数组赋值过程见代码。

代码如下:

#include<cstdio>
#include<iostream>
#include<queue>
#define inf 999999999999999999
using namespace std;
typedef long long ll;
const int N=110;
ll G[N][N];
double dis[N][N];//从(1,1)到(i,j)所需要的最短时间 
bool vis[N][N];//标记(i,j)是否做过中转点 
int b[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
struct node{
	int x,y;
	double v;
	node()
	{
	}
	node(int xx,int yy,double vv)
	{
		x=xx;
		y=yy;
		v=vv;
	}
	bool operator<(const node oth)const
	{
		return v>oth.v;
	}
};
ll ksm(ll z)//快速幂 
{
	ll ans=1,t=2;
	while(z)
	{
		if(z&1)ans*=t;
		t*=t;
		z>>=1;
	}
	return ans;
}
void dij(double v,int n,int m)
{
	for(int i=0;i<N;i++)
		for(int j=0;j<N;j++)
		{
			dis[i][j]=inf;
			vis[i][j]=0;
		}
	priority_queue<node>q;//优先队列优化 
	dis[1][1]=0;
	q.push(node(1,1,0));
	while(!q.empty())
	{
		int x=q.top().x;
		int y=q.top().y;
		q.pop();
		if(vis[x][y])continue;
		vis[x][y]=1;
		//枚举四个方向 
		for(int i=0;i<4;i++)
		{
			int tx=x+b[i][0];
			int ty=y+b[i][1];
			if(tx<1||tx>n||ty<1||ty>m)
				continue;
			double tv;
			if(G[1][1]-G[x][y]>=0)//速度变大 
				tv=1.0/(v*ksm(G[1][1]-G[x][y]));
			else//速度变小 
				tv=double(1.0)*ksm(G[x][y]-G[1][1])/v;
			if(dis[tx][ty]>dis[x][y]+tv)
			{
				dis[tx][ty]=dis[x][y]+tv;
				q.push(node(tx,ty,dis[tx][ty]));
			}
		}
	}
	return;
}
int main()
{
	double v;
	int n,m;
	while(scanf("%lf%d%d",&v,&n,&m)!=EOF)
	{
		//输入 
		for(int i=1;i<=n;i++)
			for(int j=1;j<=m;j++)
				scanf("%lld",&G[i][j]);
		//dij 
		dij(v,n,m);
		//输出 
		printf("%.2f\n",dis[n][m]);
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值