Description
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)走到A(i,j),不管走哪条路径,到A的速度Va是不变的,所以A(i,j)到B(k,l)的时间1/Va只与A与(1,1)的高度差以及初始速度V0有关,所以把时间作为边的权值建图,跑一遍最短路就好啦
d数组的初始值不能太小,刚开始用的1e9 WA了,后来改的时候又加了一个0
附上写的丑丑的Dijkstra
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<string>
#include<cstring>
#include<cmath>
#define pa pair<double,int>
using namespace std;
struct node
{
int x,y;
double z;
int next;
}t[100010];
int head[50010];
double d[500010];
int n,m,s,x,y,z,tot = 0,V;
int a[200][200];
int dist[4][2] = {{-1,0},{0,-1},{1,0},{0,1}};
void add(int x,int y,double z)
{
t[++tot].x = x;
t[tot].y = y;
t[tot].z = z;
t[tot].next = head[x];
head[x] = tot;
}
void build()
{
for(int i = 1;i <= n; ++i)
for(int j = 1;j <= m; ++j)
for(int k = 0;k < 4; ++k)
{
int x = i + dist[k][0];
int y = j + dist[k][1];
if(x > 0 && x <= n && y > 0 && y <= m)
{
double v = 1.0 / V * pow(2.0 , a[i][j] - a[1][1]);
add((i - 1) * m + j , (x - 1) * m + y , v);
}
}
}
void dij(int start)
{
priority_queue<pa,vector<pa>,greater<pa> >q;
for(int i = 1;i <= n * m; ++i) d[i] = 100000000000;
//用memset居然出了错误,我也不知道为啥,还是for好用hhh
d[start] = 0;
q.push(make_pair(0,start));
while(!q.empty())
{
int now = q.top().second;
q.pop();
for(int i = head[now];i;i = t[i].next)
{
if(d[now] + t[i].z < d[t[i].y])
{
d[t[i].y] = d[now] + t[i].z;
q.push(make_pair(d[t[i].y],t[i].y));
}
}
}
}
int main()
{
scanf("%d%d%d",&V,&n,&m);
for(int i = 1;i <= n; ++i)
for(int j = 1;j <= m; ++j)
scanf("%d",&a[i][j]);
build();
dij(1);
printf("%.2f\n",d[n * m]);
return 0;
}