首页 技术 正文
技术 2022年11月15日
0 收藏 632 点赞 3,485 浏览 2410 个字

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 ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,078
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,553
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,402
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,177
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,814
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898