Gym - 101572E Emptying the Baltic bfs加剪枝

题目传送门

题目大意:给出一幅海洋的描述,0为海平面,负数即有水,在给出的xy坐标的底部安放抽水机,问最多能有多少水。水往低处流,且八个方向都可以。

思路:bfs,记录到每一个节点有效的最低海平面,然后尝试更新周围的点。

但这道题需要优先队列,有效海平面最低的先出队,否则会TLE。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<string.h>
#include<sstream>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<bitset>
#define CLR(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long ll;
inline int rd() {
    int f = 1; int x = 0; char s = getchar();
    while (s<‘0‘ || s>‘9‘) { if (s == ‘-‘)f = -1; s = getchar(); }
    while (s >= ‘0‘&&s <= ‘9‘) { x = x * 10 + s - ‘0‘; s = getchar(); }x *= f;
    return x;
}
const int maxn = 510;
ll mp[maxn][maxn],mmp[maxn][maxn],vis[maxn][maxn];
int n, m, x, y;
int fx[8][2] = { {-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0} };
struct dian {
    int x, y;
    ll low;
    inline dian(){}
    inline dian(int x,int y,ll low):x(x),y(y),low(low){}
    inline friend bool operator <(const dian & a, const dian &b) {
        return a.low > b.low;
    }
};
inline bool check(dian &s)
{
    if (s.x <= 0 || s.y <= 0 || s.x > n || s.y > m||vis[s.x][s.y])return false;
    if (mp[s.x][s.y] > 0)return false;
    return true;
}
priority_queue<dian >q;
int main() {
    scanf("%d%d", &n, &m);
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= m; j++)
            {
                scanf("%I64d", &mp[i][j]);
            }
    }
        scanf("%d%d", &x, &y);
        if (mp[x][y] > 0) {
            printf("0\n");
            return 0;
        }
        q.push(dian(x, y,mp[x][y]));
        ll ans =0;
        mmp[x][y] = mp[x][y];
        vis[x][y] = 1;
        while (!q. empty()) {
            dian st = q.top();
            q.pop();
            for (int i = 0; i < 8; i++)
            {
                dian now = st;
                now.x += fx[i][0], now.y += fx[i][1];
                if (!check(now))continue;
                now.low= -min(-st.low, -mp[now.x][now.y]);
                if (now.low < mmp[now.x][now.y]) {
                    q.push(now);
                    mmp[now.x][now.y] = now.low;
                    vis[now.x][now.y] = 1;
                }
            }
        }
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= m; j++)
            {
                ans -= mmp[i][j];
            }
        }
        printf("%I64d\n", ans);

}

原文地址:https://www.cnblogs.com/mountaink/p/9563905.html

时间: 2024-10-03 14:39:02

Gym - 101572E Emptying the Baltic bfs加剪枝的相关文章

hdu6223 Infinite Fraction Path 2017沈阳区域赛G题 bfs加剪枝(好题)

题目传送门 题目大意:给出n座城市,每个城市都有一个0到9的val,城市的编号是从0到n-1,从i位置出发,只能走到(i*i+1)%n这个位置,从任意起点开始,每走一步都会得到一个数字,走n-1步,会得到一个长度为n的数列,输出能得到的最大的数列(当成数字). 思路: 一个数字肯定是最高位越大,这个数字本身就越大,所以肯定第一位要取最大值,在这一位取最大值的时候后面每一位都要尽量最大,所以想到bfs. 但是bfs肯定要剪枝,怎么剪枝呢? 1.按照思路,我要取每一位尽可能大的值,所以某一个状态的某

hdu 1044(bfs+dfs+剪枝)

Collect More Jewels Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6739    Accepted Submission(s): 1564 Problem Description It is written in the Book of The Lady: After the Creation, the cruel

【深搜加剪枝五】HDU 1010 Tempter of the Bone

Tempter of the BoneTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 64326    Accepted Submission(s): 17567 Problem Description The doggie found a bone in an ancient maze, which fascinated him a l

HDU 1728 逃离迷宫 单方向BFS 或者DFS加剪枝

逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 18519    Accepted Submission(s): 4463 Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越,有些地

HDU 4474(神奇的BFS+强剪枝)

 HDU - 4474 Yet Another Multiple Problem Time Limit: 20000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Description There are tons of problems about integer multiples. Despite the fact that the topic is not original, the content is hi

ACM: Gym 101047E Escape from Ayutthaya - BFS

Gym 101047E Escape from Ayutthaya Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Practice Description standard input/output Ayutthaya was one of the first kingdoms in Thailand, spanning since its foundation in 1350 to it

Codeforces Gym 100187E E. Two Labyrinths bfs

E. Two Labyrinths Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/problem/E Description A labyrinth is the rectangular grid, each of the cells of which is either free or wall, and it's possible to move only between free

UVA - 11882 Biggest Number(dfs+bfs+强剪枝)

题目大意:给出一个方格矩阵,矩阵中有数字0~9,任选一个格子为起点,将走过的数字连起来构成一个数,找出最大的那个数,每个格子只能走一次. 题目分析:DFS.剪枝方案:在当前的处境下,找出所有还能到达的点的个数,若当前数字的长度加上个数仍小于目前最优答案的长度,则剪去:若长度相等,则将所有还能到达的数字按从大到小排序后连到当前数字上,如果还比目前最优解小,则减去.找出所有还能到达的点的过程用BFS实现. 1 #pragma comment(linker, "/STACK:1024000000,10

HDU1664 (bfs +数论剪枝)

Different Digits Time Limit: 10000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1129 Accepted Submission(s): 290 Problem Description Given a positive integer n, your task is to find a positive integer m, which