Emptying the Baltic

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

解题思路:一开始我用了类似spfa的思路,不过有数据点超时了,后来改为优先队列, 保证先出队列的是当前已处理的最深的节点



#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>

using namespace std;

#define LL long long
const int INF = 0x3f3f3f3f;

int n, m, x, y;
int a[505][505];
int vis[505][505],h[505][505];
int dir[8][2] = { {1,0},{0,1},{0,-1},{-1,0},{1,1},{-1,-1},{1,-1},{-1,1} };

struct node
{
	int x, y, h;
	bool operator<(const node &a)const
	{
		return h > a.h;
	}
}pre, nt;

int main()
{
	while (~scanf("%d%d", &n, &m))
	{
		priority_queue<node>q;
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= m; j++) scanf("%d", &a[i][j]);
		scanf("%d%d", &x, &y);
		memset(vis, 0, sizeof vis);
		pre = { x,y,a[x][y] };
		q.push(pre);
		vis[x][y] = 1;
		h[x][y] = a[x][y];
		while (!q.empty())
		{
			pre = q.top();
			q.pop();
			for (int i = 0; i < 8; i++)
			{
				int xx = pre.x + dir[i][0];
				int yy = pre.y + dir[i][1];
				if (xx<1 || yy<1 || xx>n || yy>m|| a[xx][yy] >= 0||vis[xx][yy]) continue;
				int tmp = max(pre.h, a[xx][yy]);
				h[xx][yy] = tmp;
				vis[xx][yy] = 1;
				nt = { xx,yy,tmp };
				q.push(nt);
			}
		}
		LL ans = 0;
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= m; j++)
				ans += -1LL * h[i][j];
		printf("%lld\n", ans);
	}
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值