题目链接:

https://cn.vjudge.net/problem/POJ-3037

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)处的初始速度v,和高度矩阵的大小r,c
接下来输入一个高度矩阵
问从(1,1)处出发,走到(r,c)所需要的最短时间
时间的计算规则是,从A点到B点,假设高度是HA,HB,到达B点的速度是A点的速度乘以2^(HA-HB),而该过程所需要的时间是到达B点度的倒数 解题思路
读完题感觉像是搜索题的最短路,由于花费时间不同,导致同层拓展的结点优先级不同,再采用优先队列保证每次弹出的是最短时间结点,求
出最短时间,有点模拟的意思。
不过仔细读题发现,其实不用像模拟那样随时更新速度,因为只要有了(1,1)点的速度,其他点的速度都能求出来。假设a,b,c三点的高度
分别是h1,h2,h3, a点的速度为v1,求v2和v3。易得v2=v1*2^(h1-h2);将v2代入v3=v2*2(h2-h3)=v1*2^(h1-h2)*2(h2-h3)=v1*2(h1-h3);故有
有了初始速度其他点的速度是固定的,那么可以将其看成是一个邻接矩阵,求(1,1)到(r,c)的最短路径,权值就是每条边花费的时间。
这里使用SPFA,
1.初始化二维数组t为最短路径的估计值,除了(1,1)外,全部都是INF
在这里需要注意的是,因为是浮点数造成之前的int最大值不适用了,要变为原来的二倍
2.在队列中加入一个起始顶点
每次弹出一个顶点,以该点为中心,四周的最短距离如果能够被该点松弛,就更新。
如果四周的点没有被用过,就加入队列
3.最终队列为空的时候,计算出从(1,1)处出发,走到(r,c)所需要的最短时间。
POJ精度问题使用%.2f输出,用%.2lf精度太小,舍入不正确
*/
#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath>
using namespace std; const double INF = 0x7fffffff;//double是int的二倍,故0x3f3f3f3f的二倍
const int maxn = ;
int dir[][] = {,, ,, -,, ,-};
struct NODE{
int x, y;
};
bool vis[maxn][maxn];
double e[maxn][maxn];
double t[maxn][maxn];
double v;
int r, c;
void SPFA(); int main()
{
while(scanf("%lf%d%d", &v, &r, &c) != EOF) {
for(int i = ; i <= r; i++) {
for(int j = ; j <= c; j++) {
scanf("%lf", &e[i][j]);
}
}
SPFA();
}
return ;
} void SPFA() {
for(int i = ; i <= r; i++) {
for(int j = ; j <= c; j++) {
t[i][j] = INF;
}
}
t[][] = ; memset(vis, , sizeof(vis));
vis[][] = ;
queue<NODE> q;
q.push((NODE){,});
while(!q.empty()) {
NODE tmp = q.front();
q.pop();
vis[tmp.x][tmp.y] = ;//清除标记
double w = 1.0 / (v * pow(2.0, e[][] - e[tmp.x][tmp.y]));
for(int k = ; k < ; k++) {
int tx = tmp.x + dir[k][];
int ty = tmp.y + dir[k][]; if(tx < || tx > r || ty < || ty > c)
continue;
if(t[tmp.x][tmp.y] < INF && t[tx][ty] > t[tmp.x][tmp.y] + w) {
t[tx][ty] = t[tmp.x][tmp.y] + w;
if(!vis[tx][ty]) {
vis[tx][ty] = ;
q.push((NODE){tx,ty});
}
}
}
} printf("%.2f\n", t[r][c]);
}

POJ 3037 Skiing(如何使用SPFA求解二维最短路问题)的更多相关文章

  1. POJ 2029 Get Many Persimmon Trees (二维树状数组)

    Get Many Persimmon Trees Time Limit:1000MS    Memory Limit:30000KB    64bit IO Format:%I64d & %I ...

  2. poj 3625 Building Roads(最小生成树,二维坐标,基础)

    题目 //最小生成树,只是变成二维的了 #define _CRT_SECURE_NO_WARNINGS #include<stdlib.h> #include<stdio.h> ...

  3. POJ 2029 Get Many Persimmon Trees 【 二维树状数组 】

    题意:给出一个h*w的矩形,再给出n个坐标,在这n个坐标种树,再给出一个s*t大小的矩形,问在这个s*t的矩形里面最多能够得到多少棵树 二维的树状数组,求最多能够得到的树的时候,因为h,w都不超过50 ...

  4. POJ - 3037 Skiing SPFA

    Skiing Bessie and the rest of Farmer John's cows are taking a trip this winter to go skiing. One day ...

  5. POJ 3037 Skiing

    Skiing Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4810   Accepted: 1287   Special ...

  6. POJ 3037 Skiing(Dijkstra)

    Skiing Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4668   Accepted: 1242   Special ...

  7. [BZOJ1579][Usaco2009 Feb]Revamping Trails 道路升级(二维最短路问题)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1579 分析: 设d[i][j]表示从1走到i.改了j条边的最短路径长度 如果设i相连的 ...

  8. [luogu4479][BJWC2018]第k大斜率【二维偏序+二分+离散化+树状数组】

    传送门 https://www.luogu.org/problemnew/show/P4479 题目描述 在平面直角坐标系上,有 n 个不同的点.任意两个不同的点确定了一条直线.请求出所有斜率存在的直 ...

  9. POJ_1195 Mobile phones 【二维树状数组】

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u013912596/article/details/33802561 题目链接:id=1195&qu ...

随机推荐

  1. 复制命令(XCOPY)

    XCOPY 命令: // 描述: 将文件或目录(包括子目录)从一个位置复制到另一个位置. // 语法: Xcopy <Source> [<Destination>] [/w] ...

  2. 数据库-SQL语句:删除和修改语句-列类型-列约束

    使用MySQL客户端连接服务器的两种方式: (1)交互模式: ——查 mysql.exe  -h127.0.0.1  -uroot  -p mysql   -uroot (2)脚本模式:——增删改 m ...

  3. java动态生成验证码图片

    package cn.lijun.checkimg; import java.awt.image.BufferedImage;import java.io.BufferedReader; import ...

  4. ubuntu无法打开software-center

    ubuntu无法打开software-center BUG: 在ubuntu14.04LTS版本下,点击软件中心图标,过了一会软件未能启动,没有动静.用命令行启动报如下错误: perrin@Littl ...

  5. jq的事件对象

  6. jdk8新特性(详解)

    最近在复习外加看点面试题,jdk8的新特性虽然在项目用用到过一两个,准备系统的了解一下jdk8的常用新特性 一:Lambd表达式 也可称为闭包         引入函数式编程到Java中 为了使现有函 ...

  7. 使用 PLSQL 提示动态执行表不可访问,本会话的自动统计被禁止

    使用PLSQL,第一次执行表的select操作的时候,提示"动态执行表不可访问,本会话的自动统计被禁止",如上图: 这种问题,一看就是当前连接用户没有对sys用户下的表v$sess ...

  8. 微信小程序基础

    前言 什么是微信小程序,它是一种轻量级的APP,它与常规App来说,无需下载安装即可使用,它嵌于微信App中,要使用微信小程序你只需要搜索一下微信小程序的名称就好,如近期的"Google的画 ...

  9. MySQL备份---lvm snapshot

    正常安装(缺点要锁表) 1, 创建一个LV(逻辑卷) , 把MySQL的数据目录放到这个LV上 /var/lib/mysql 对这个LV做快照, 从快照备份数据 删除快照 非正常安装 1,创建LV 2 ...

  10. GMM基础

    一.单成分单变量高斯模型 二.单成分多变量高斯模型 若协方差矩阵为对角矩阵且对角线上值相等,两变量高斯分布的等值线为圆形. 若协方差矩阵为对角矩阵且对角线上值不等,两变量高斯分布的等值线为椭圆形, 长 ...