GYM - 101572E Emptying the Baltic 优先队列+BFS

题目链接: GYM - 101572 E

题目大意

n*m的地图, 每个格子有一个海拔高度, 海拔<0的地方有水, 现在在(x, y)最深处放一个抽水机, 问最多能抽多少水, 水只能从高出往低处流

思路

一开始我想到用普通队列BFS, 但是因为水可以往八个方向流, 如果先出队列的不出最深的一个点的话, 原本能流过去的水可能就流不过去了, 所以用优先队列, 保证先出队列的是当前已处理的最深的节点

代码

Accepted 124 ms 5200 KB

#include <bits/stdc++.h>
using namespace std;

const int maxn = 600;
int s[maxn][maxn], n, m;
bool vist[maxn][maxn];
int dx[] = {1, -1, 0, 0, -1, -1, 1, 1};
int dy[] = {0, 0, -1, 1, -1, 1, -1, 1};
struct P
{
    int x, y;
    P(int i, int j):x(i), y(j) {}
    P(){}
    bool operator < (const P&b) const
    {
        return s[x][y] > s[b.x][b.y];
    }
};

int main()
{
    cin >> n >> m;
    for(int i=0; i<n; ++i)
        for(int j=0; j<m; ++j) scanf("%d", &s[i][j]);
    int x, y;
    scanf("%d%d", &x, &y);
    --x, --y;

    priority_queue<P> que;
    que.push(P(x, y));
    vist[x][y] = 1;
    while(!que.empty())
    {
        P now = que.top(); que.pop();
        for(int i=0; i<8; ++i)
        {
            int nx = now.x + dx[i];
            int ny = now.y + dy[i];
            if(0<=nx && nx<n && 0<=ny && ny<m && !vist[nx][ny])
            {
                vist[nx][ny] = 1;
                s[nx][ny] = max(s[now.x][now.y], s[nx][ny]);
                que.push(P(nx, ny));
            }
        }
    }
    long long ans = 0;
    for(int i=0; i<n; ++i) for(int j=0; j<m; ++j) if(s[i][j] < 0) ans += -s[i][j];
    cout << ans << endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值