Skiing

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. 1 3 3
  2. 1 5 3
  3. 6 3 5
  4. 2 4 3

Sample Output

  1. 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
 
 
题意:以邻接矩阵形式读入各点高度,滑到某点速度为v0*2^(原点高度-某点高度),求从左上滑到右下用时。
思路:SPFA。1.0/v即为上一点到当前点用时,扩展到右下点找出最短用时。注意到某点用时一定要提前记录,如果在扩展时现求的话会有许多重复计算,导致TLE。。(pow(,)的计算相当耗时)
ps:SPFA O(kE),k小于等于2(已证明)E为边数,适合稀疏图(可含负边),加优化Dij O((n+m)logn)适合稠密图。在练习最短路时一直会拿这两者来比较,这次用SPFA来写,感觉和BFS相似,却也有不同之处,SPFA会用标记数组记录进入队列的点,而当点出队列时会取消标记(以后可能会再用到),进而松弛更新。
 
  1. #include<stdio.h>
  2. #include<math.h>
  3. #include<float.h> //DBL_MAX头文件
  4. #include<queue>
  5. using namespace std;
  6.  
  7. int a[][],b[][];
  8. double dis[][],sp[][];
  9. int t[][]={{,},{,},{-,},{,-}};
  10. int v0,r,c;
  11. struct Node{
  12. int x,y;
  13. }node;
  14. double spfa(int x,int y)
  15. {
  16. int i,j;
  17. queue<Node> q;
  18. for(i=;i<=r;i++){
  19. for(j=;j<=c;j++){
  20. dis[i][j]=DBL_MAX;
  21. }
  22. }
  23. dis[x][y]=;
  24. node.x=x;
  25. node.y=y;
  26. q.push(node);
  27. b[x][y]=;
  28. while(q.size()){
  29. int fx=q.front().x;
  30. int fy=q.front().y;
  31. q.pop();
  32. b[fx][fy]=;
  33. for(i=;i<;i++){
  34. int tx=fx+t[i][];
  35. int ty=fy+t[i][];
  36. double t=sp[fx][fy];
  37. if(tx<||ty<||tx>r||ty>c) continue;
  38. if(dis[fx][fy]+t<dis[tx][ty]){
  39. dis[tx][ty]=dis[fx][fy]+t;
  40. if(!b[tx][ty]){
  41. node.x=tx;
  42. node.y=ty;
  43. q.push(node);
  44. b[tx][ty]=;
  45. }
  46. }
  47. }
  48. }
  49. return dis[r][c];
  50. }
  51.  
  52. int main()
  53. {
  54. int i,j;
  55. scanf("%d%d%d",&v0,&r,&c);
  56. for(i=;i<=r;i++){
  57. for(j=;j<=c;j++){
  58. scanf("%d",&a[i][j]);
  59. sp[i][j]=1.0/(v0*pow(2.0,a[][]-a[i][j])); //提前记录
  60. }
  61. }
  62. printf("%.2f\n",spfa(,));
  63. return ;
  64. }

POJ - 3037 Skiing SPFA的更多相关文章

  1. POJ 3037 Skiing(如何使用SPFA求解二维最短路问题)

    题目链接: https://cn.vjudge.net/problem/POJ-3037 Bessie and the rest of Farmer John's cows are taking a ...

  2. POJ 3037 Skiing

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

  3. POJ 3037 Skiing(Dijkstra)

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

  4. Skiing POJ 3037 很奇怪的最短路问题

    Skiing POJ 3037 很奇怪的最短路问题 题意 题意:你在一个R*C网格的左上角,现在问你从左上角走到右下角需要的最少时间.其中网格中的任意两点的时间花费可以计算出来. 解题思路 这个需要发 ...

  5. POJ 3037 SPFA

    题意: 思路: 我们可以发现 到每个点的速度是一样的 那这就成水题了-. 裸的SPFA跑一哈 搞定 //By SiriusRen #include <cmath> #include < ...

  6. POJ 1860(spfa)

    http://poj.org/problem?id=1860 题意:汇率转换,与之前的2240有点类似,不同的是那个题它去换钱的时候,是不需要手续费的,这个题是需要手续费的,这是个很大的不同. 思路: ...

  7. poj 3621 二分+spfa判负环

    http://poj.org/problem?id=3621 求一个环的{点权和}除以{边权和},使得那个环在所有环中{点权和}除以{边权和}最大. 0/1整数划分问题 令在一个环里,点权为v[i], ...

  8. poj 1847( floyd && spfa )

    http://poj.org/problem?id=1847 一个水题,用来熟悉熟悉spfa和floyd的. 题意:有m条的铁路,要从x,到y, 之后分别就是条铁路与其他铁路的交点.第一个输入的为有n ...

  9. ACM: POJ 3259 Wormholes - SPFA负环判定

     POJ 3259 Wormholes Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu   ...

随机推荐

  1. struts2的分页标签

    1.准备tld文件 <?xml version="1.0" encoding="UTF-8" standalone="no"?> ...

  2. 空间Rm的任意两个范数都互相等价

  3. php总结5——常量、文件上传

    5.1常量 系统常量: PHP_OS  操作系统 PHP_VERSION    php版本 PHP_SAPI    运行方式 自定义常量: define("常量名称"," ...

  4. 【学员管理系统】0x01 班级信息管理功能

    [学员管理系统]0x01 班级信息管理功能 写在前面 项目详细需求参见:Django项目之[学员管理系统] 视图函数: 我们把所有的处理请求相关的函数从 urls.py中拿出来,统一放在一个叫view ...

  5. ubuntun下安装sublime text

    Sublime Text 3 是一款轻量级.跨平台的文本编辑器.可安装在ubuntu,Windows和MAC OS X上高级文本编辑软件,有一个专有的许可证,但该程序也可以免费使用,无需做逆向工程.如 ...

  6. MV45AOZZ 销售订单增强点

    [转自 http://blog.csdn.net/zhongguomao/article/details/6712580]choose the table VBAP or VBAK ( dependi ...

  7. GstAppSrc简介

    Description The appsrc element can be used by applications to insert data into a GStreamer pipeline. ...

  8. 采集练习(十) php 获得电视节目预告---数据来自搜视网

    前几天逛湖南卫视,偶然间发现它的网站上也有节目预告,一看源码,居然是来自搜视网的xml,于是就想获得它的数据(页面直接ajax加载估计会有跨域问题) 前阵子也写过另一个方法获得 节目预告(采集练习(七 ...

  9. ubuntu 更新软件命令

    安装软件最好加权限(sudo) --default-timeout=100 设置超时时间100秒 install -U setuptools 表示更新安装setuptools sudo pip3 -- ...

  10. 使用 eslint 和 editorconfig 规范代码

    项目中使用eslint 为什么使用eslint : 为了保持代码风格的统一 在做vue项目的时候, 基本上都会使用 vue-cli 脚手架去创建一个vue 项目,里面可以选择使用eslint 代码检测 ...