Gym 101572E Emptying the Baltic(优先队列)

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


思路:从(x,y)开始bfs,扩展到的点v[tu][tv] = max(v[tu][tv], v[u][v])。但是不能用普通的bfs。需要加优先队列。(

打开链接因为水可以往八个方向流, 如果先出队列的不出最深的一个点的话, 原本能流过去的水可能就流不过去

了, 以用优先队列, 保证先出队列的是当前已处理的最深的节点。


代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e3+5;
int v[maxn][maxn], n, m, x, y;
bool vis[maxn][maxn];
struct node
{
    int x, y;
    node() {}
    node(int xx, int yy):x(xx), y(yy) {}
    bool operator <(const node &a) const
    {
        return v[x][y] > v[a.x][a.y];
    }
};

void bfs()
{
    priority_queue<node> pq;
    pq.push(node(x, y));
    vis[x][y] = 1;
    while(!pq.empty())
    {
        node u = pq.top(); pq.pop();
        int next[8][2] = {0, 1, 0, -1, 1, 0, -1, 0, 1, 1, 1, -1, -1, 1, -1, -1};
        for(int i = 0; i < 8; i++)
        {
            int tx = u.x+next[i][0];
            int ty = u.y+next[i][1];
            if(tx >= 1 && tx <= n && ty >= 1 && ty <= m && !vis[tx][ty])
            {
                vis[tx][ty] = 1;
                v[tx][ty] = max(v[tx][ty], v[u.x][u.y]);
                pq.push(node(tx, ty));
            }
        }
    }
}

int main(void)
{
    while(cin >> n >> m)
    {
        memset(vis, 0, sizeof(vis));
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= m; j++)
                scanf("%d", &v[i][j]);
        x, y;
        scanf("%d%d", &x, &y);
        bfs();
        ll ans = 0;
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= m; j++)
                if(v[i][j] < 0)
                    ans += -v[i][j];
        printf("%lld\n", ans);
    }
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值