Application of G on the priority queue figure - Millionaire Madness Kattis - millionairemadness

G - Millionaire Madness

                    Kattis - millionairemadness   

The main idea: that is, to give you a map of n * m (int) represents the height of the goods), can move up and down a time frame, but to high altitude (than the original place) where have to use a ladder.
Demand from the most left point to point most of the lower right, at least take long ladder? .
Topic Analysis:
is a bfs
have to use the priority queue, which is a very handy tool.
Bfs is traversed, from the head of the queue each time (Top) out, and then push the partial discharge is a difference between the previous point, it will take a negative zero.
So to be able to take assurance from the head of the queue is minimal. In order to achieve a minimum, and then to the end it can be output.
Drop Code:

 #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
typedef  long long ll;
#define INF 0x3f3f3f3f
#include<bits/stdc++.h>
#include <cmath>
#include <math.h>
#define PI 3.1415926535
const int maxn=1e7+1;
void acc_ios()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
}
struct node
{
    int x,y;
    ll val;
    bool operator<(const node &a)const
    {
        return a.val<val;
    }
};
int nextx[5]={0,0,1,-1};
int nexty[5]={1,-1,0,0};
int a[1010][1010];
int vis[1010][1010];
int n,m;
ll ans;
void bfs()
{
    memset(vis,0,sizeof(vis));
    priority_queue<node>Q;
    Q.push((node){0,0,0});
    while(!Q.empty())
    {
        node k=Q.top();
        Q.pop();
        if(vis[k.x][k.y]) continue;
        ans=max(ans,k.val);
        vis[k.x][k.y]=1;
        if(k.x==n-1&&k.y==m-1)
        {
            return ;
        }
        for(int i=0;i<4;i++)
        {
            int nx=k.x+nextx[i];
            int ny=k.y+nexty[i];
            if(nx<0||nx>=n||ny<0||ny>=m) continue;
           if(vis[nx][ny])
           {
               continue;
           }
           Q.push((node){nx,ny,max(0,a[nx][ny]-a[k.x][k.y])});
        }
    }
    }
    int main()
    {
        acc_ios();
        ans=0;
        cin>>n>>m;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                cin>>a[i][j];
            }
        }
        bfs();
        cout<<ans<<endl;
        return 0;
    }
```cpp
在这里插入代码片


Released nine original articles · won praise 0 · Views 137

Guess you like

Origin blog.csdn.net/luoxutimberjack/article/details/104349304