POJ 3037 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

一个r行c列的区域,每个地方有一个高度,从a->b速度会变成2^(a-b),每个地方都有一个时间消耗,求从左上角到右下角的最短时间。

可以简单的推算出每个小格子的速度vij = v*2^(height(i,j)-height(1,1)),其中v是初始的速度,height(i,j)是要求速度的格子的高度,这样的话每个地方的时间都可以确定下来,可以用最短路径算法,也可以用搜索(bfs+heap),第一次a过用的最短路径,也是我第一次在题目中写spfa算法,貌似跟广搜很像。


//以下是spfa
#include <iostream>
#include <queue>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <cfloat>
using namespace std;
const int M = 105;
double mapp[M][M];
int r, c;
double v;//初始速度可能不是整数,这一点费了我好长时间
bool bo[M][M];
double tim[M][M];
double ans;
int mv[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};
struct Node
{
	int x, y;
};
double dis[M][M];
void spfa()
{
	memset(bo, false, sizeof(bo));
	queue<Node> q;
	int xt, yt;
	Node tmp, nex;
	tmp.x = 1;
	tmp.y = 1;
	for(int i=1;i<=r;i++)
		for(int j=1;j<=c;j++)
			dis[i][j] = DBL_MAX;
	dis[1][1] = 0;
	q.push(tmp);
	while(!q.empty())
	{
		tmp = q.front();
		q.pop();
		bo[tmp.x][tmp.y] = false;
		for(int i=0;i<4;i++)
		{
			xt = tmp.x + mv[i][0];
			yt = tmp.y + mv[i][1];
			if(xt<1||yt<1||xt>r||yt>c)
				continue;
			if(dis[xt][yt]>tim[tmp.x][tmp.y]+dis[tmp.x][tmp.y])
			{
				dis[xt][yt] = tim[tmp.x][tmp.y] + dis[tmp.x][tmp.y];
				if(!bo[xt][yt])
				{
					bo[xt][yt] = true;
					nex.x = xt;
					nex.y = yt;
					q.push(nex);
				}
			}
		}
	}
}

int main()
{
	double spe;
	scanf("%lf%d%d", &v, &r, &c);
	for(int i=1;i<=r;i++)
		for(int j=1;j<=c;j++)
		{
			scanf("%lf", &mapp[i][j]);
			if(i==1&&j==1)
				tim[i][j] = 1 / v;
			else
			{
				spe = v * pow(2, mapp[1][1] - mapp[i][j]);
				tim[i][j] = 1 / spe;
			}
		}
	spfa();
	printf("%.2f", dis[r][c]);
	return 0;
}



//以下是搜索
#include <iostream>
#include <queue>
#include <cmath>
#include <cstring>
#include <cstdio>
using namespace std;
const int M = 105;
double mapp[M][M];
int r, c;
double v;
bool bo[M][M];
double tim[M][M];
double ans;
int mv[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};
struct Node
{
	int x, y;
	double tim;
	bool operator < (const Node &a)const
	{
		return tim > a.tim;
	}
};
void bfs()
{
	memset(bo, false, sizeof(bo));
	Node tmp, nex;
	int xt, yt;
	priority_queue<Node> q;
	tmp.x = 1;
	tmp.y = 1;
	tmp.tim = tim[1][1];
	bo[1][1] = true;
	q.push(tmp);
	while(!q.empty())
	{
		tmp = q.top();
		q.pop();
		// printf("%d %d %f\n", tmp.x, tmp.y, tmp.tim);
		if(tmp.x==r&&tmp.y==c)
		{
			ans = tmp.tim - tim[r][c];
			return;
		}
		for(int i=0;i<4;i++)
		{
			xt = tmp.x + mv[i][0];
			yt = tmp.y + mv[i][1];
			if(xt<1||yt<1||xt>r||yt>c)
				continue;
			if(!bo[xt][yt])
			{
				bo[xt][yt] = true;
				nex.x = xt;
				nex.y = yt;
				nex.tim = tmp.tim + tim[xt][yt];
				q.push(nex);
			}
		}
	}
}
int main()
{
	double spe;
	scanf("%lf%d%d", &v, &r, &c);
	for(int i=1;i<=r;i++)
		for(int j=1;j<=c;j++)
		{
			scanf("%lf", &mapp[i][j]);
			if(i==1&&j==1)
				tim[i][j] = 1 / v;
			else
			{
				spe = v * pow(2, mapp[1][1] - mapp[i][j]);
				tim[i][j] = 1 / spe;
			}
		}
	bfs();
	printf("%.2f", ans);
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值