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 = ; int x = ; char s = getchar();
while (s<'' || s>'') { if (s == '-')f = -; s = getchar(); }
while (s >= ''&&s <= '') { x = x * + s - ''; s = getchar(); }x *= f;
return x;
}
const int maxn = ;
ll mp[maxn][maxn],mmp[maxn][maxn],vis[maxn][maxn];
int n, m, x, y;
int fx[][] = { {-,},{,},{,},{,},{,-},{,-},{-,-},{-,} };
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 <= || s.y <= || s.x > n || s.y > m||vis[s.x][s.y])return false;
if (mp[s.x][s.y] > )return false;
return true;
}
priority_queue<dian >q;
int main() {
scanf("%d%d", &n, &m);
for (int i = ; i <= n; i++)
{
for (int j = ; j <= m; j++)
{
scanf("%I64d", &mp[i][j]);
}
}
scanf("%d%d", &x, &y);
if (mp[x][y] > ) {
printf("0\n");
return ;
}
q.push(dian(x, y,mp[x][y]));
ll ans =;
mmp[x][y] = mp[x][y];
vis[x][y] = ;
while (!q. empty()) {
dian st = q.top();
q.pop();
for (int i = ; i < ; i++)
{
dian now = st;
now.x += fx[i][], now.y += fx[i][];
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] = ;
}
}
}
for (int i = ; i <= n; i++)
{
for (int j = ; j <= m; j++)
{
ans -= mmp[i][j];
}
}
printf("%I64d\n", ans); }
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步,会 ...
- E - Emptying the Baltic Kattis - emptyingbaltic (dijkstra堆优化)
题目链接: E - Emptying the Baltic Kattis - emptyingbaltic 题目大意:n*m的地图, 每个格子有一个海拔高度, 当海拔<0的时候有水. 现在在(x ...
- hdu 1044(bfs+dfs+剪枝)
Collect More Jewels Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- HDU 1254 推箱子(BFS加优先队列)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1254 推箱子 Time Limit: 2000/1000 MS (Java/Others) Me ...
- ACM: Gym 101047E Escape from Ayutthaya - BFS
Gym 101047E Escape from Ayutthaya Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I6 ...
- Gym 101617J Treasure Map(bfs暴力)
http://codeforces.com/gym/101617/attachments 题意:给出一个图,每个顶点代表一个金矿,每个金矿有g和d两个值,g代表金矿初始的金子量,d是该金矿每天的金子量 ...
- Codeforces Gym 100187E E. Two Labyrinths bfs
E. Two Labyrinths Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/prob ...
- HDU 5510 Bazinga 暴力匹配加剪枝
Bazinga Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5510 ...
- UVA - 11882 Biggest Number(dfs+bfs+强剪枝)
题目大意:给出一个方格矩阵,矩阵中有数字0~9,任选一个格子为起点,将走过的数字连起来构成一个数,找出最大的那个数,每个格子只能走一次. 题目分析:DFS.剪枝方案:在当前的处境下,找出所有还能到达的 ...
随机推荐
- LookupError: unknown encoding: cp65001解决办法
一.之前手上做的一个web项目,漏洞频发,服务器用的是菜鸟云服务器,那个应急响应中心不错,想不到乌云倒了,白帽子竟然被阿里系养了,题外话了,首先感谢白帽子提的漏洞,同时也感慨自己安全知识,以及意识的薄 ...
- close、flush、read、readline、seek、tell、truncate、write的使用
1.close关闭文件 f1= open("ha.log","r+",encoding="utf-8") data = f1.read() ...
- IDEA创建Maven项目一直显示正在加载的问题
在用idea创建maven项目的时候 有时候会出现下面这种情况 出现原因 IDEA根据maven archetype的本质,其实是执行mvn archetype:generate命令,该命令执行时,需 ...
- 使用ServerSocket建立聊天服务器(一)
-------------siwuxie095 工程名:TestMyServerSocket 包名:com.siwuxie095.socket ...
- C++重载流插入和流输出运算符
demo: /* Name: 重载输入输出流运算符使力代码 Copyright: qianshou Author: zhaozhe Date: 07/12/13 00:11 Description: ...
- 算法Sedgewick第四版-第1章基础-005一封装输入(可以文件,jar包里的文件或网址)
1. package algorithms.util; /*********************************************************************** ...
- 20.LIBRARY_PATH和LD_LIBRARY_PATH环境变量的区别
转载:https://www.cnblogs.com/panfeng412/archive/2011/10/20/library_path-and-ld_library_path.html LIBRA ...
- CF 671D Roads in Yusland
弄完之后点进去一看,竟然是div1的D题……最近真是天天被题虐哭 推荐这一篇博客 https://www.cnblogs.com/Sakits/p/8085598.html 感觉讲清楚了,也是基本照着 ...
- CF609E Minimum spanning tree for each edge
原来觉得是一个LCT,感觉自己瞬间傻掉…… 考虑到先做一个最小生成树求出做最小生成树的代价$ans$,顺便标记一下树边和非树边,把边按照输入$id$排序回去之后扫,如果扫到一条树边,那么此时的答案就是 ...
- java反射中,Class.forName和classloader的区别
http://blog.csdn.net/qq_27093465/article/details/52262340