zoukankan      html  css  js  c++  java
  • POJ

    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
     
    这个题用vector的话会T
    下面是vector是T的代码:
    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<algorithm>
    #include<queue>
    #include<stack>
    #include<set>
    #include<map>
    #include<vector>
    #include<cmath>
    #define Inf 0x3f3f3f3f
    
    const int maxn=1e5+5;
    typedef long long ll;
    using namespace std;
    ll ksm(ll x,ll y)
    {
        ll ans=1;
        while(y)
        {
            if(y&1)
            {
                ans=ans*x;
            }
            x*=x;
            y>>=1;
        }
        return ans;
    }
    struct node
    {
        int to;
        double w;
        bool friend operator < (node x,node y)
        {
            return x.w>y.w;
        }
    };
    int Map[10005];
    
    vector<node>vec[10005];
    double dis[10005];
    int vis[10005];
    int V,R,C;
    int dir[4][2]={{0,1},{0,-1},{-1,0},{1,0}};
    bool check(int x,int y)
    {
        if(x>=1&&x<=R&&y>=1&&y<=C)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    void init()
    {
        for(int t=1;t<=R*C;t++)
        {
            dis[t]=100000000000;
        }
    }
    
    void Dijkstra(int s)
    {
        node st;
        st.to=s;
        st.w=0;
        priority_queue<node>q;
        q.push(st);
        dis[s]=0;
        while(!q.empty())
        {
            node now=q.top();
            q.pop();
            if(vis[now.to])continue;
            vis[now.to]=1;
            
            int len=vec[now.to].size();
            for(int t=0;t<len;t++)
            {
                node tto=vec[now.to][t];
    
                if(vis[tto.to]==0&&tto.w+dis[now.to]<dis[tto.to])
                {
                    tto.w=tto.w+dis[now.to];
                    dis[tto.to]=tto.w;
                    q.push(tto);
                }
            }
        }
    }
    
    int main()
    {
    //    std::ios::sync_with_stdio(false);
        scanf("%d%d%d",&V,&R,&C);
        init();
        for(int t=1;t<=R;t++)
        {
            for(int j=1;j<=C;j++)
            {
                scanf("%d",&Map[(t-1)*C+j]);
            }
        }
        for(int t=1;t<=R;t++)
        {
            for(int j=1;j<=C;j++)
            {
                for(int k=0;k<4;k++)
                {
                    int xx=t+dir[k][0];
                    int yy=j+dir[k][1];
                    if(check(xx,yy))
                    {
                      node s;
                      s.to=(xx-1)*C+yy;
                      s.w=1.0/V*ksm(2,Map[(t-1)*C+j]-Map[1]);                 
                      vec[(t-1)*C+j].push_back(s);
                    }
                }
            }
        }
        Dijkstra(1);
        printf("%.2f
    ",dis[R*C]);
        
        return 0;
    }


    AC的是邻接表的
    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<algorithm>
    #include<queue>
    #include<stack>
    #include<set>
    #include<map>
    #include<vector>
    #include<cmath>
    
    const int maxn=1e4+5;
    typedef long long ll;
    using namespace std;
    
    struct edge
    {
        int u,v;
        double w;
        int next;
    }edge[maxn*10];
    
    struct node
    {
        int pos;
        double w;
        node(int x,double y)
        {
            pos=x;
            w=y;
        }
        bool friend operator <(node x,node y)
        {
            return x.w>y.w;
        }
    };
    int n,m,s,x,y,z,tot = 0,V;
    bool check(int x,int y)
    {
        if(x>=1&&x<=n&&y>=1&&y<=m)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    int head[10005];
    double d[10005];
    int vis[10005];
    int a[10005];
    int dist[4][2] = {{-1,0},{0,-1},{1,0},{0,1}};
    
    void add(int u,int v,double w)
    {
        edge[++tot].u=u;
        edge[tot].v=v;
        edge[tot].w=w;
        edge[tot].next=head[u];
        head[u]=tot;
        return ;
    }
    
    void Dijkstra(int s)
    {
        priority_queue<node>q;
        d[s]=0;
        q.push(node(s,0));
        while(!q.empty())
        {
            node now=q.top();
            q.pop();
            //cout<<now.pos<<endl;
            if(vis[now.pos])continue;
            vis[now.pos]=1;
    
        for(int i=head[now.pos];i!=-1;i=edge[i].next)
        {
             int ne=edge[i].v;
             double ww=edge[i].w;
            if(d[now.pos]+ww<d[ne])
            {
                d[ne]=d[now.pos]+ww;
                q.push(node(ne,d[ne]));
            }
        }
        }
        return ;
    }
    int main()
    {
        scanf("%d%d%d",&V,&n,&m);
        memset(head,-1,sizeof(head));
        memset(vis,0,sizeof(vis));
       
        for(int t=1;t<=n*m;t++)
        {
            d[t]=1000000000000;
        }
        for(int i = 1;i <= n; ++i)
            for(int j = 1;j <= m; ++j)
                scanf("%d",&a[(i-1)*m+j]);
         for(int i = 1;i <= n; ++i)
            for(int j = 1;j <= m; ++j)
                for(int k = 0;k < 4; ++k)
                {
                    int x = i + dist[k][0];
                    int y = j + dist[k][1];
                    if(check(x,y))
                    {
                        double v = 1.0 / V * pow(2.0 , a[(i-1)*m+j] - a[1]);
                        add((i-1)*m+j,(x-1)*m+y,v);
                    }
                }
        Dijkstra(1);
        printf("%.2f
    ",d[n * m]);
        return 0;
    }
  • 相关阅读:
    布隆过滤器原理与应用场景
    【转】程序员的世界真的很难懂~
    IDEA 2019.2.4 破解安装教程
    【转】只有程序员才能看得懂的段子
    Linux 正则表达式
    【转】雷军做程序员时写的博客,很强大!
    如何同步 Linux 集群系统时间
    百度共享云盘
    Shell 脚本 test 命令详解
    Linux 命令大全
  • 原文地址:https://www.cnblogs.com/Staceyacm/p/11266128.html
Copyright © 2011-2022 走看看