Skiing POJ 3037 very strange shortest path problem

Skiing POJ 3037 very strange shortest path problem

The meaning of problems

Meaning of the questions: Are you a R * C in the upper left corner of the grid, to ask you now come to the minimum time required for the lower right corner from the top left corner where the time spent between any two points in the grid can be calculated.

Problem-solving ideas

The need to find a law, that is, from top left to any other point, which path you select, the arrival rate at that point is fixed.

For example, one of the following matrix:

1 5 3

6 3 5

2 4 3

We want to calculate the value can be found for the velocity of the point 2, then, \ [V_2 V_1 = 2 ^ {1-2} * * \] , is such that the path \ [l-> 6-> 2 \] , then \ [V_2 V_1 = ^ * 2 * 2 ^ {1-6} {6-2} * V_1 = 2 ^ {1-2} \] . So that we can know the speed of the other points.

Code implementation is very strange, this is a reference to the big brother \ (lhm \) students Code, a very bad student, and later turn clinical medicine, and his team want to do playing the game, but bless him, to find their own like a professional, after all, it is also a professional turn into a computer professional, to turn professional but also have their own feelings.

Code

#include<iostream>
#include<cstdio>
#include<queue>
#include<cmath>
using namespace std;
struct node{
    int x,y;
    double time;
    bool operator < (const node& tmp) const
    {
        return time > tmp.time;
    }
};
double v;
int n,m;
int book[110][110],a[110][110];
priority_queue<node>q;
int next[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
void bfs()
{
    while(!q.empty()) q.pop();
    q.push((node){1,1,0.0});
    while(!q.empty())
    {
        node t=q.top(); q.pop();
        if(book[t.x][t.y]==1) continue;
        book[t.x][t.y]=1;
        if(t.x==n&&t.y==m) 
        {
            printf("%.2f\n",t.time);
            return ;
        }
        for(int i=0;i<4;i++)
        {
            int tx=t.x+next[i][0];
            int ty=t.y+next[i][1];
            if(tx>=1&&tx<=n&&ty<=m&&ty>=1&&book[tx][ty]==0) 
            {
                double tt=t.time+1.0/(pow(2.0,(a[1][1]-a[t.x][t.y]))*v);
                q.push((node){tx,ty,tt}); //注意这里加入的点没有进行标记
            }
        }
    }
    return ;
}
int main()
{
    while(scanf("%lf%d%d", &v, &n, &m)!=EOF)
    {
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            scanf("%d",&a[i][j]);
        }
        bfs();  
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/alking1001/p/11600511.html
Recommended