暑假训练 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

#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cmath>
#include<vector>
#include<list>
#include<stack>
#include<queue>
#include<deque>
#include<string>
#include<algorithm>
#define MST(a, b) memset(a, b, sizeof a)
using namespace std;
typedef long long ll;
const int maxn = 120;
const double INF = 1e10;
int dir[4][2] = {{-1, 0}, {1, 0}, {0, 1}, {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 sofa(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();
        //cout<<now.x<<' '<<now.y<<endl;
        vis[now.x][now.y] = false;
        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;
    sofa(0, 0);
    printf("%.2f\n", dis[r-1][c-1]);
    return 0;
}

小结:
emmm,当时看博客还以为不会真的用bfs或dfs来求最短路,这题还真是bfs求最短路,有一点就是任何一个点的速度是确定的,只要求与左上角的高度差值就能算出速度,然后转换成图,再用bfs求解。。。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值