【POJ】3037Skiing(Dijkstra+权值计算)

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5867 Accepted: 1557 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. 

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

Source

USACO 2005 October Gold

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<vector>
#include<algorithm>
#include<cmath>
#define inf 0x3f3f3f3f3f
#define maxn 10010
#define pb push_back
using namespace std;
int R,C;
double v[110][100];
int height[110][110];
int dr[]={-1,1,0,0};
int dc[]={0,0,-1,1};

struct HeapNode
{
    double d;
    int u;
    HeapNode(double d,int u):d(d),u(u) {}
    bool operator < (const HeapNode &ags) const
    {
        return d>ags.d;
    }
};

struct Edge
{
    int from,to;
    double dist;
    Edge(int f,int t,double d):from(f),to(t),dist(d) {}
};

struct Dijkstra
{
    int n,m;

    vector<Edge> edges;
    vector<int > G[maxn];
    bool done[maxn];
    double d[maxn];
    int p[maxn];
    void init(int n)
    {
        this->n=n;
        for(int i=0; i<n; i++)
            G[i].clear();
        edges.clear();
    }

    void AddEdge(int from,int to,double dist)
    {
        edges.push_back(Edge(from,to,dist));
        m=edges.size();
        G[from].push_back(m-1);
    }

    void dijkstra()
    {
        priority_queue<HeapNode> Q;
        for(int i=0;i<n;i++) d[i]=inf;
        d[0]=0.0;
        memset(done,0,sizeof(done));
        Q.push(HeapNode(d[0],0));


        while(!Q.empty())
        {
            HeapNode x=Q.top();
            Q.pop();
            int u=x.u;
            if(done[u])
                continue;
            done[u]=true;

            for(int i=0; i<G[u].size(); i++)
            {
                Edge &e=edges[G[u][i]];
                if(d[e.to]>d[u]+e.dist)
                {
                    d[e.to]=d[u]+e.dist;
                    p[e.to]=G[u][i];
                    Q.push(HeapNode(d[e.to],e.to));
                }
            }
        }
    }
} DJ;



int main()
{
    cin>>v[0][0]>>R>>C;
    for(int i=0; i<R; i++)
        for(int j=0; j<C; j++)
        {
            cin>>height[i][j];
            if(i!=0||j!=0)
                v[i][j] = v[0][0]*pow(2.0,height[0][0]-height[i][j]);
        }
    DJ.init(R*C);
    for(int r=0; r<R; r++)
        for(int c=0; c<C; c++)
        {
            for(int d=0;d<4;d++)
            {
                int nr=r+dr[d];
                int nc=c+dc[d];
                if(nr>=0&&nr<R&&nc>=0&&nc<C)
                {
                    DJ.AddEdge(r*C+c,nr*C+nc,1.0/v[r][c]);
                }
            }
        }
    DJ.dijkstra();
    printf("%.2lf\n",DJ.d[R*C-1]);
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值