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 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
 
 
题意:以邻接矩阵形式读入各点高度,滑到某点速度为v0*2^(原点高度-某点高度),求从左上滑到右下用时。
思路:SPFA。1.0/v即为上一点到当前点用时,扩展到右下点找出最短用时。注意到某点用时一定要提前记录,如果在扩展时现求的话会有许多重复计算,导致TLE。。(pow(,)的计算相当耗时)
ps:SPFA O(kE),k小于等于2(已证明)E为边数,适合稀疏图(可含负边),加优化Dij O((n+m)logn)适合稠密图。在练习最短路时一直会拿这两者来比较,这次用SPFA来写,感觉和BFS相似,却也有不同之处,SPFA会用标记数组记录进入队列的点,而当点出队列时会取消标记(以后可能会再用到),进而松弛更新。
 
#include<stdio.h>
#include<math.h>
#include<float.h> //DBL_MAX头文件
#include<queue>
using namespace std; int a[][],b[][];
double dis[][],sp[][];
int t[][]={{,},{,},{-,},{,-}};
int v0,r,c;
struct Node{
int x,y;
}node;
double spfa(int x,int y)
{
int i,j;
queue<Node> q;
for(i=;i<=r;i++){
for(j=;j<=c;j++){
dis[i][j]=DBL_MAX;
}
}
dis[x][y]=;
node.x=x;
node.y=y;
q.push(node);
b[x][y]=;
while(q.size()){
int fx=q.front().x;
int fy=q.front().y;
q.pop();
b[fx][fy]=;
for(i=;i<;i++){
int tx=fx+t[i][];
int ty=fy+t[i][];
double t=sp[fx][fy];
if(tx<||ty<||tx>r||ty>c) continue;
if(dis[fx][fy]+t<dis[tx][ty]){
dis[tx][ty]=dis[fx][fy]+t;
if(!b[tx][ty]){
node.x=tx;
node.y=ty;
q.push(node);
b[tx][ty]=;
}
}
}
}
return dis[r][c];
} int main()
{
int i,j;
scanf("%d%d%d",&v0,&r,&c);
for(i=;i<=r;i++){
for(j=;j<=c;j++){
scanf("%d",&a[i][j]);
sp[i][j]=1.0/(v0*pow(2.0,a[][]-a[i][j])); //提前记录
}
}
printf("%.2f\n",spfa(,));
return ;
}

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. Java for LeetCode 117 Populating Next Right Pointers in Each Node II

    Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...

  2. PAT 天梯赛 L2-028. 秀恩爱分得快 【数据处理】

    题目链接 https://www.patest.cn/contests/gplt/L2-028 思路 0.只处理被询问的情侣的亲密度,否则会超时 1.要注意输入数字要用字符串,还要标记性别 因为 输出 ...

  3. 从动态库的def文件生成lib文件

    以sqlite3为例,下载的文件中只有def文件,没有lib文件,想使用静态方式调用dll的情况下,就需要额外的.h文件和.lib文件存在. .h文件可以从官方下载的sqlite-amalgamati ...

  4. loader与plugin,module与chunk,compiler与compilation

    loader将各类型的文件转为webpack能处理的有效模块(module) 插件处理范围更广的任务,例如打包优化.压缩等 module程序的离散功能块,一个文件对应一个module chunk若干m ...

  5. 20145239杜文超 《Java程序设计》第8周学习总结

    20145239 <Java程序设计>第8周学习总结 教材学习内容总结 通用API 日志API 1.java.util.logging包提供了日志功能相关类与接口,使用日志的起点是logg ...

  6. win10安装tomcat7

    下载Tomcat 安装tomcat tomcat7是绿色软件,解压后即可使用,请大家先将tomcat解压到合适的位置(建议整个路径都是英文路径),下载 apache-tomcat-7.0.79-win ...

  7. ASP.NET MVC3中的路由系统

    MVC中,用户访问的地址并不映射到服务器中对应的文件,而是映射到对应Control里对应的ActionMethod,由ActionMethod来决定返回用户什么样的信息.而把用户访问的地址对应到对应的 ...

  8. 最短路径问题----Dijkstra算法的解释

    先上图: 现在要找到地点V1到其余各个地点的最短路径(图中数字的单位默认为km.).有一个原则是:永远找最小,确保无更小. 第一步:v1->v1,v1->v2,...v1->v7的距 ...

  9. Android USB 开发详解

    Android USB 开发详解 先附上 Android USB 官方文档 Android通过两种模式支持各种 USB 外设和 Android USB 附件(实现Android附件协议的硬件):USB ...

  10. redis数据

    毫无疑问,Redis开创了一种新的数据存储思路,使用Redis,我们不用在面对功能单调的数据库时,把精力放在如何把大象放进冰箱这样的问题上,而是利用Redis灵活多变的数据结构和数据操作,为不同的大象 ...