POJ3037 Skiing. spfa

三周之前的题。来填个坑。。。拖了这么久终于A了。。。
题意:不多解释了。
思路:经过我们研究发现,从某一点出发的速度(即从某点出发到周围四个点的耗时)只和该点与(1,1)这个点的高度差有关。
用spfa来解决(似乎还可以用dijkstra 有空再实践一下吧。。。)
这次自己写了一遍,如果不看题解会有很多WA点:
1、首先inf 0x3f3f3f3f是不够大的 至少999…9(十个9)才行。
2、没搞懂spfa队列优化的原理,应该是:某一点可以多次入队。
3、忘记乘以速度。。。(wa成狗

下面上AC代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#define ll long long
#define cl(a,b) memset(a,b,sizeof(a))
#define maxn 110
using namespace std;
const double inf = 9999999999;
double v;
int r,c;
int map[maxn][maxn];
double dis[maxn][maxn];//这里maxn是耗时 是小数形式
bool con[maxn][maxn];//bool判断有没有来过
int nextt[4][2]= {{1,0},{0,-1},{-1,0},{0,1}};
struct node {
	int x,y;
};
queue<node> q;
void spfa() {
	node cur,nxt;//当前这个,下一个
	dis[1][1]=0;//到1 1 的距离为0
	cur.x=1,cur.y=1;
	q.push(cur);
	while(!q.empty()) {
		cur=q.front();
		q.pop();
		con[cur.x][cur.y]=0;//一个点出队后可以再次入队!!! 
		//这里你少了速度!!!!!! 
		double t=1.0/(v*pow(2,map[1][1]-map[cur.x][cur.y]));
		for(int i=0; i<4; i++) {
			nxt.x=cur.x+nextt[i][0];
			nxt.y=cur.y+nextt[i][1];
			if(nxt.x<1||nxt.y<1||nxt.x>r||nxt.y>c) {
				continue;//若越界 不行
			} else {
				if(dis[nxt.x][nxt.y]>dis[cur.x][cur.y]+t) {
					dis[nxt.x][nxt.y]=dis[cur.x][cur.y]+t;
					if(con[nxt.x][nxt.y]==0) {
						q.push(nxt);
						con[nxt.x][nxt.y]=1;
					}
				}
			}
		}
	}
}
int main() {
	scanf("%lf%d%d",&v,&r,&c);
	for(int i=1; i<=r; i++) { //输入地图
		for(int j=1; j<=c; j++) {
			scanf("%d",&map[i][j]);
			dis[i][j]=inf;
			con[i][j]=0;
		}
	}
	spfa();
	printf("%.2f\n",dis[r][c]);

	return 0;
}

下面上原题:
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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值