Gym - 101572E Emptying the Baltic 优先队列

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_38013346/article/details/82262865

Description

n m 0 就是说给出一个n*m的地形块,0表示海平面,负数表示地面在海水下,正数表示是陆地,且高出海平面。
( x , y ) 现在在(x,y)处放上一个排水的设备,问其能排出多少的水,对于一个位置,水能够从八个方向进来。


Input

1 h , w 500 1\leq h,w\leq 500
0 i < h 0\leq i < h
0 j < m 0\leq j< m


Output

总的的排水量


Solution

水往低处流。
于是需要优先最低的,即水位最深的。
j i = m i n ( d e p i , d e p j ) d e p 考虑j \to i 的水流量 = min(dep_i,dep_j) dep是离海平面的距离。


Codes

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 550;
struct Node{
    int x,y;
    ll val;
    Node(){}
    Node(int _x,int _y,ll _val) {x = _x;y = _y;val = _val;}
    bool operator < (const Node &a)const {
        return val < a.val;
    }
};

priority_queue<Node> qu;
int n,m,maze[maxn][maxn];
bool vis[maxn][maxn];
int main()
{
    while(~scanf("%d%d",&n,&m)) {
        while(!qu.empty()) qu.pop();
        memset(vis,0,sizeof(vis));
        for(int i=0;i<n;i++) {
            for(int j=0;j<m;j++) {
                scanf("%d",&maze[i][j]); 
                if(maze[i][j] >= 0) {
                    maze[i][j] = 0;
                }
                else {
                    maze[i][j] = -maze[i][j];
                }
            }
        }
        int x,y;
        scanf("%d%d",&x,&y);x--;y--;
        qu.push(Node(x,y,maze[x][y]));
        vis[x][y] = true;
        ll res = 0;
        while(!qu.empty()) {
            Node now = qu.top();qu.pop();
            res += now.val;
            for(int i=-1;i<=1;i++) for(int j=-1;j<=1;j++) {
                int mx = now.x + i,my = now.y + j;
                if(mx<0 || mx >= n || my<0 || my >= m || vis[mx][my] || maze[mx][my]==0) continue;
                vis[mx][my] = 1;
                // maze[mx][my] = min((ll)maze[mx][my],now.val);
                qu.push(Node(mx,my,min(now.val,(ll)maze[mx][my])));
            }
        }
        // for(int i=0;i<n;i++) {
        //     for(int j=0;j<m;j++) if(maze[i][j] && vis[i][j]) res += maze[i][j];
        // }
        printf("%lld\n",res);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_38013346/article/details/82262865