题目链接:http://poj.org/problem?id=3037
Time Limit: 1000MS Memory Limit: 65536K

 

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

Problem solving report:

Description: 给出一个网格,每个点都有一个高度。给出初始速度,每次可以从一个点走到上下左右四个相邻的点。每走到一个点,速度会发生变化:设原来的点的高度为A,下一个点高度为B,则速度变为原来的速度*2^(A-B),走到下一个点所用时间为1/原来的速度。问从点(1,1)走到点(r,c)所用的最小时间。。
Problem solving: 对于每个点从该点出发的速度是恒定的,例如从a->b->c;则c出发的速度就是V*2^(A-B)*2^(B-C)=V*2^(A-C),所以直接求最短路径就可以了,从一维变成了二维的。

Accepted Code:

#include <cmath>
#include <queue>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 105;
const int MAXM = 10005;
const double inf = 0x3f3f3f3f3f3f3f3f;
bool vis[MAXN][MAXN];
double tar[MAXN][MAXN];
int mp[MAXN][MAXN], r, c;
int dir[][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
struct edge {
    int x, y;
    double v;
    edge(int x_ = 0, int y_ = 0, double v_ = 0) : x(x_), y(y_), v(v_) {};
}p;
inline void init() {
    for (int i = 1; i <= r; i++)
        for (int j = 1; j <= c; j++)
            tar[i][j] = inf;
    memset(vis, false, sizeof(vis));
}
inline double Spfa(int x, int y, int v) {
    init();
    tar[x][y] = 0;
    queue <edge> Q;
    vis[x][y] = true;
    Q.push(edge(x, y, v * 1.0));
    while (!Q.empty()) {
        p = Q.front();
        Q.pop();
        vis[p.x][p.y] = false;
        for (int i = 0; i < 4; i++) {
            int tx = p.x + dir[i][0];
            int ty = p.y + dir[i][1];
            if (tx >= 1 && tx <= r && ty >= 1 && ty <= c) {
                if (tar[tx][ty] > tar[p.x][p.y] + 1.0 / p.v) {
                    tar[tx][ty] = tar[p.x][p.y] + 1.0 / p.v;
                    if (!vis[tx][ty]) {
                        vis[tx][ty] = true;
                        Q.push(edge(tx, ty, p.v * (pow(2, mp[p.x][p.y] - mp[tx][ty]))));
                    }
                }
            }
        }
    }
    return tar[r][c];
}
int main() {
    int v;
    scanf("%d%d%d", &v, &r, &c);
    for (int i = 1; i <= r; i++)
        for (int j = 1; j <= c; j++)
            scanf("%d", &mp[i][j]);
    printf("%.2f\n", Spfa(1, 1, v));
    return 0;
}