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

 

题意:第一行,给出一个初始速度和矩阵的长宽。每个点的速度为V*2^(A-B) ,A是出发点B是到达的地方然后到达每个点的时间是上一个点的速度的倒数。

题解:可以看出每个点的速度都与第一个点有关,因为V*2^(A-B)*2^(B-C)它就等于V*2^(A-C),所以每个点的速度都可以用第一个点算出来。然后把每个点对应到下一个点的时间存到这个点的位置上。详细请看代码。

 

#include <iostream>
#include <cstring>
#include <queue>
#include <cmath>
#include <cfloat>
#include <cstdio>
using namespace std;
const double inf = 0x3f3f3f3f;
const int MAX = 200;
int v,r,c;
int vis[MAX][MAX];
double mp[MAX][MAX],t[MAX][MAX],dis[MAX][MAX];// mp要用double 待会用到pow函数要不然报错
int mv[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
struct hh{
	int x,y;
};
void spfa(){
	memset(vis,0,sizeof(vis));
	for(int i=1;i<=r;i++){// 赋一个很大的值给double类型的数组,要用这种形式,要不然用下面那种形式,不对。
		for(int j=1;j<=c;j++){
			dis[i][j] = DBL_MAX;
		}
	}
	//memset(dis,inf,sizeof(dis));		
	queue<hh> q;
	hh tmp,nex;
	int xx,yy;
	tmp.x=1;
	tmp.y=1;
	dis[1][1]=0;//注意赋值为0,dis数组表示别的点到这个点需要的时间,不包括这个点到别的点的时间,也就是不包括这个点所存的时间
	q.push(tmp);
	while(!q.empty()){
		tmp=q.front();
		q.pop();
		vis[tmp.x][tmp.y]=0;// 注意要赋值0,要不然会错,因为这个点我们可能需要重新走
		for (int i = 0; i < 4;i++){
			xx=tmp.x+mv[i][0];
			yy=tmp.y+mv[i][1];
			if(xx<1|yy<1||xx>r||yy>c) continue;
			if(dis[xx][yy]>t[tmp.x][tmp.y]+dis[tmp.x][tmp.y]){
				dis[xx][yy]=t[tmp.x][tmp.y]+dis[tmp.x][tmp.y];
				if(!vis[xx][yy]){
					vis[xx][yy]=1;
					nex.x=xx;
					nex.y=yy;
					q.push(nex);
				}
			}
		}
	}
}
int main(){
	cin >> v >> r >> c;
	for (int i = 1; i <= r;i++){
		for (int j = 1; j <= c;j++){
			cin >> mp[i][j];
			t[i][j]=1/(v*(pow(2,mp[1][1]-mp[i][j])));//计算每个点到下一个点所需要的时间,存到这个点上
		}
	}
	spfa();
	printf("%.2lf\n",dis[r][c]);
	return 0;
}

 

感觉spfa有点像bfs,各路大神有没有感觉!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

心脏dance

如果解决了您的疑惑,谢谢打赏呦

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值