Skiing
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 3432 | Accepted: 955 | 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
题意:给出一个网格,每个点都有一个高度。给出初始速度,每次可以从一个点走到上下左右四个相邻的点。每走到一个点,速度会发生变化:设原来的点的高度为A,下一个点高度为B,则速度变为原来的速度*2^(A-B),走到下一个点所用时间为1/原来的速度。问从点(1,1)走到点(r,c)所用的最小时间。
思路:法一:BFS+heap,用时小的优先。
AC代码:
#include <cstring>
#include <string>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <iostream>
#define max2(a,b) ((a) > (b) ? (a) : (b))
#define min2(a,b) ((a) < (b) ? (a) : (b))
using namespace std;
const double INF=9999999999; //这里设少了会wa
struct node
{
int x,y;
double step,v;
bool operator < (const node &a) const
{
return step>a.step;
}
};
int d[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
int map[105][105];
double minstep[105][105]; //minstep[x][y]保存到达点(x,y)所用的最小时间
int r,c;
double v;
bool judge(int x,int y)
{
if(x>=1&&x<=r&&y>=1&&y<=c)
return true;
return false;
}
double BFS()
{
for(int i=1;i<=r;i++)
for(int j=1;j<=c;j++)
minstep[i][j]=INF;
priority_queue<node> Q;
node cur,next;
cur.x=1;
cur.y=1;
cur.v=v;
cur.step=0;
Q.push(cur);
while(!Q.empty())
{
cur=Q.top();
Q.pop();
if(cur.x==r&&cur.y==c)
return cur.step;
for(int i=0;i<4;i++)
{
next.x=cur.x+d[i][0];
next.y=cur.y+d[i][1];
if(!judge(next.x,next.y))
continue;
next.v=cur.v*pow(2.0,map[cur.x][cur.y]-map[next.x][next.y]);
next.step=cur.step+1.0/cur.v;
if(minstep[next.x][next.y]>next.step)
{
minstep[next.x][next.y]=next.step;
Q.push(next);
}
}
}
return -1;
}
int main()
{
scanf("%lf%d%d",&v,&r,&c);
for(int i=1;i<=r;i++)
for(int j=1;j<=c;j++)
scanf("%d",&map[i][j]);
printf("%.2f\n",BFS());
return 0;
}
法二:对于每个点从该点出发的速度是恒定的,例如从a->b->c;则c出发的速度就是V*2^(A-B)*2^(B-C)=V*2^(A-C); 所以c与相邻点的距离就知道了。直接求最短路径就可以了。用spfa。。
AC代码:
#include <cstring>
#include <string>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <iostream>
#define max2(a,b) ((a) > (b) ? (a) : (b))
#define min2(a,b) ((a) < (b) ? (a) : (b))
using namespace std;
const double INF=9999999999; //这里设少了会wa
struct node
{
int x,y;
};
int d[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
int map[105][105];
double dis[105][105]; //dis[i][j]表示点(1,1)到点(i,j)的距离
int r,c;
double v;
bool vis[105][105];
bool judge(int x,int y)
{
if(x>=1&&x<=r&&y>=1&&y<=c)
return true;
return false;
}
double spfa()
{
queue<node>Q;
node cur,next;
for(int i=1;i<=r;i++)
for(int j=1;j<=c;j++)
dis[i][j]=INF;
cur.x=1;
cur.y=1;
dis[1][1]=0;
vis[1][1]=true;
Q.push(cur);
while(!Q.empty())
{
node cur=Q.front();
Q.pop();
vis[cur.x][cur.y]=false;
double t=1.0/(v*pow(2.0,map[1][1]-map[cur.x][cur.y])); //点(cur.x,cur.y)到相邻点的距离
for(int i=0;i<4;i++)
{
next.x=cur.x+d[i][0];
next.y=cur.y+d[i][1];
if(!judge(next.x,next.y)) continue;
if(dis[next.x][next.y]>dis[cur.x][cur.y]+t)
{
dis[next.x][next.y]=dis[cur.x][cur.y]+t;
if(!vis[next.x][next.y])
{
vis[next.x][next.y]=true;
Q.push(next);
}
}
}
}
return dis[r][c];
}
int main()
{
scanf("%lf%d%d",&v,&r,&c);
for(int i=1;i<=r;i++)
for(int j=1;j<=c;j++)
scanf("%d",&map[i][j]);
printf("%.2f\n",spfa());
return 0;
}