Skiing
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 5228 | Accepted: 1389 | Special Judge |
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.
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.
* 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
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
Source
思路:这个题用到物理上势能与动能转化的知识点, 因为不是一维的,所以不会用Dijkstra,这个题有两种思路
1. 利用BFS+优先队列, 即每次从时间最少的点搜索,直到搜到目标点,值得注意的是,将这个点的vis设置为“1”的条件是这个点是最少时间点,而不是走到哪个哪个设为“1”;
2. 用spfa; 话不多说上代码
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<math.h>
#include<queue>
using namespace std;
const int maxn=110;
const int INF=0x3f3f3f3f;
int map[maxn][maxn], vis[maxn][maxn];
int n, m, dir[4][2]={{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
double v;
struct node{
int x, y;
double t;
bool operator < (const node& tmp) const
{
return t>tmp.t;
}
};
void BFS(){
priority_queue<node> q;
q.push((node){1, 1, 0.0});
while(q.size())
{
node it=q.top();
q.pop();
if(vis[it.x][it.y]) continue;
vis[it.x][it.y]=1;
if(it.x==n && it.y==m)
{
printf("%.2f\n", it.t);
return ;
}
for(int i=0; i<4; i++)
{
int tx=it.x+dir[i][0];
int ty=it.y+dir[i][1];
if(tx<1 || tx>n || ty<1 || ty>m)
continue;
if(!vis[tx][ty])
{
double t=it.t + 1.0/(pow(2.0, (map[1][1]-map[it.x][it.y])*1.0)*v);
q.push((node){tx, ty, t});
}
}
}
}
int main(){
scanf("%lf%d%d", &v, &n, &m);
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
scanf("%d", &map[i][j]);
BFS();
return 0;
}
#include<stdio.h>
#include<math.h>
#include<queue>
using namespace std;
const double INF=0x3f3f3f3f3f;//注意因为是double类型, 所以要设的大一点
const int maxn = 110;
int mp[maxn][maxn] , vis[maxn][maxn];
double time[maxn][maxn];
int dir[4][2]={{1, 0}, {-1, 0}, {0, -1}, {0, 1}};
struct node{
int x, y;
};
void spfa(int n, int m, double v){
time[1][1]=0;
queue<node> q;
q.push((node){1, 1});
vis[1][1]=1;
while(!q.empty())
{
node it=q.front();
q.pop();
vis[it.x][it.y]=0;
double t=1.0/(v*pow(2.0, mp[1][1]-mp[it.x][it.y]));
for(int i=0; i<4; i++)
{
int tx=it.x+dir[i][0];
int ty=it.y+dir[i][1];
if(tx<1 || tx>n || ty<1 || ty>m)
continue;
if(time[tx][ty]>time[it.x][it.y]+t)
{
time[tx][ty]=time[it.x][it.y]+t;
if(!vis[tx][ty])
{
q.push((node){tx, ty});
vis[tx][ty]=1;
}
}
}
}
}
int main(){
double v;
int r, c;
scanf("%lf%d%d", &v, &r, &c);
for(int i=1; i<=r; i++)
{
for(int j=1; j<=c; j++)
{
scanf("%d", &mp[i][j]);
time[i][j]=INF;
vis[i][j]=0;
}
}
spfa(r, c, v);
printf("%.2f\n", time[r][c]);
return 0;
}