本文已参与「新人创作礼」活动,一起开启掘金创作之路。
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
先大概翻一下题意吧:
有一个人要滑雪,雪山有不同的高度,雪山范围是r*c,当他从高度为A的雪山移动到高度为B的雪山,则他在B雪山上的速度等于在A雪山上的速度乘以2^(A-B),问你从雪山的左上角滑到右下角的最短时间。
分析:类比我们物理学过的重力势能只与高度有关,不难想到这道题目中滑雪的速度也至于这个人所在的雪山高度有关,可以设一开始的位置为高度0点,然后预处理出来所有雪山的速度,最后用一个spfa就行了,注意,本题数据类型为double,我们对double类型赋值为无穷大不能用memset,而且double类型的无穷大也不应该是0x3f3f3f3f,而是比这个数更大的数。
下面上代码
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
using namespace std;
typedef pair<int,int> PII;
const int N=103;
int dx[4]={0,0,1,-1},dy[4]={1,-1,0,0};
double t[N][N],T[N][N];
int h[N][N];
bool vis[N][N];
int r,c;
double V;
int main()
{
cin>>V>>r>>c;
for(int i=1;i<=r;i++)
for(int j=1;j<=c;j++)
{
scanf("%d",&h[i][j]);
t[i][j]=pow(2,(h[i][j]-h[1][1]))/V;
T[i][j]=900000000000000;
}
queue<PII> q;
q.push({1,1});
T[1][1]=0;
while(q.size())
{
int tx=q.front().first,ty=q.front().second;
q.pop();
vis[tx][ty]=false;
for(int i=0;i<4;i++)
{
int nx=tx+dx[i],ny=ty+dy[i];
if(nx>=1&&nx<=r&&ny>=1&&ny<=c&&T[nx][ny]>T[tx][ty]+t[tx][ty])
{
T[nx][ny]=T[tx][ty]+t[tx][ty];
if(!vis[nx][ny])
{
q.push({nx,ny});
vis[nx][ny]=true;
}
}
}
}
printf("%.2f",T[r][c]);
return 0;
}
还有要注意的就是pow函数是计算机中自带的函数,返回类型是double。