Skiing(POJ - 3037)

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

题意:

似乎是一道最短路径的题目,要求从最左上角到最右下角的最短距离,每次移动的时间是当前(移动前)的速度*2^(ha-hb)的倒数。

思路:

易推导得知这个速度的变化是具有传递性的,比如a,b,c三点的海拔分为别h1,h2,h3,而a为初始点速度为v0,那么vb=v0*2^(h1-h2),vc=vb*2^(h2-h3)=v0*2^(h1-h3),所以只需要知道和左上角的点的海拔差,就可以利用spfa算出最短路径。
坑点就是……0x3f3f3f3f的值是不够大的,卡了我好久,要比这个还要大才可以,还有就是poj的精度问题,%f输出就可以了。

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <map>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <list>
#include <cmath>
#include <algorithm>
#define MST(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int MAXN = 100 + 20;
const double INF = 10000000000;
int dir[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
struct node {
    int x, y;
    node(int _x, int _y) {
        x = _x;
        y = _y;
    }
    node() {}
} ;
bool vis[MAXN][MAXN];
double dis[MAXN][MAXN], v[MAXN][MAXN], v0;
int h[MAXN][MAXN], r, c;
void spfa(int x,int y) {
    queue <node> q;
    for (int i = 0; i <= r; i++)
        for (int j = 0; j <= c; j++) {
            dis[i][j] = INF;
            vis[i][j] = false;
        }
    node now, next;
    q.push({x, y});
    vis[x][y] = true;
    dis[x][y] = 0;
    while (!q.empty()) {
        now = q.front();
        q.pop();
        vis[now.x][now.y] = false;
//        cout << now.x << " " << now.y << " " << dis[now.x][now.y] << endl;
        for (int i = 0; i < 4; i++) {
            next.x = now.x + dir[i][1];
            next.y = now.y + dir[i][0];
            if (next.x < 0 || next.y < 0 || next.x >= r || next.y >= c) continue;
            double len = 1.0 / v[now.x][now.y];
            if (dis[next.x][next.y] > dis[now.x][now.y] + len) {
                dis[next.x][next.y] = dis[now.x][now.y] + len;
                if (!vis[next.x][next.y]) {
                    vis[next.x][next.y] = true;
                    q.push(next);
                }
            }
        }
    }
}
int main() {
    scanf("%lf %d %d", &v0, &r, &c);
    for (int i = 0; i < r; i++)
        for (int j = 0; j < c; j++) {
            scanf("%d", &h[i][j]);
            v[i][j] = v0 * pow(2.0, h[0][0] - h[i][j]);
        }
    v[0][0] = v0;
//    for (int i = 0; i < r; i++)
//        for (int j = 0; j < c; j++) {
//            printf("%06.4lf", v[i][j]);
//            if (j == c - 1) printf("\n");
//            else printf("  ");
//        }
    spfa(0, 0);
    printf("%.2f\n", dis[r - 1][c - 1]);
}

猜你喜欢

转载自blog.csdn.net/white_yasha/article/details/80531486
今日推荐