Problem - 3329

  用pfs,将淹没时间调整回来,然后用并查集,时间倒序插入点。

代码如下:

 #include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue> using namespace std; const int N = ;
const int dx[] = { -, , , };
const int dy[] = { , -, , };
bool vis[N][N];
int mat[N][N]; typedef pair<int, int> PII;
typedef pair<int, PII> PIII;
priority_queue<PIII> pq;
int n, m;
inline bool inmat(int x, int y) { return <= x && x < n && <= y && y < m;} void adjust() {
int ht, cx, cy, nx, ny;
while (!pq.empty()) {
PIII tmp = pq.top();
pq.pop();
ht = -tmp.first, cx = tmp.second.first, cy = tmp.second.second;
//cout << ht << ' ' << cx << ' ' << cy << endl;
for (int i = ; i < ; i++) {
nx = cx + dx[i], ny = cy + dy[i];
if (!inmat(nx, ny)) continue;
if (vis[nx][ny]) continue;
mat[nx][ny] = max(ht, mat[nx][ny]);
pq.push(PIII(-mat[nx][ny], PII(nx, ny)));
vis[nx][ny] = true;
}
}
//for (int i = 0; i < n; i++) {
//for (int j = 0; j < m; j++) cout << mat[i][j] << ' ';
//cout << endl;
//}
} struct MFS {
int fa[N * N];
void init() { for (int i = ; i < N * N; i++) fa[i] = i;}
int find(int x) { return fa[x] = fa[x] == x ? x : find(fa[x]);}
bool merge(int x, int y) {
int fx = find(x), fy = find(y);
if (fx == fy) return false;
fa[fx] = fy;
return true;
}
} mfs; int work() {
int ret = -;
for (int i = ; i < n; i++) {
for (int j = ; j < m; j++) {
pq.push(PIII(mat[i][j], PII(i, j)));
mat[i][j] = -;
}
}
PIII tmp;
int nid = , mg = , nx, ny;
bool sp = false;
mfs.init();
while (!pq.empty()) {
int ct = pq.top().first;
while (!pq.empty() && ct == pq.top().first) {
tmp = pq.top();
int h = tmp.first, x = tmp.second.first, y = tmp.second.second;
pq.pop();
mat[x][y] = nid++;
for (int i = ; i < ; i++) {
nx = x + dx[i], ny = y + dy[i];
if (!inmat(nx, ny)) continue;
if (mat[nx][ny] == -) continue;
mg += mfs.merge(mat[x][y], mat[nx][ny]);
}
}
if (sp) {
if (mg == nid - ) ret = ct, sp = false;
} else {
if (mg != nid - ) sp = true;
}
}
return ret;
} int main() {
int cas = ;
while (~scanf("%d%d", &n, &m) && (n || m)) {
while (!pq.empty()) pq.pop();
memset(vis, , sizeof(vis));
for (int i = ; i < n; i++) {
for (int j = ; j < m; j++) {
scanf("%d", &mat[i][j]);
if (i == || i == n - || j == || j == m - ) {
pq.push(PIII(-mat[i][j], PII(i, j)));
vis[i][j] = true;
}
}
}
//cout << "??" << endl;
adjust();
int ans = work();
if (ans == -) printf("Case %d: Island never splits.\n", cas++);
else printf("Case %d: Island splits when ocean rises %d feet.\n", cas++, ans);
}
return ;
}

——written by Lyon

hdu 3329 The Flood (Flood Fill + MFSet)的更多相关文章

  1. python自动化开发-[第七天]-面向对象

    今日概要: 1.继承 2.封装 3.多态与多态性 4.反射 5.绑定方法和非绑定方法 一.新式类和经典类的区别 大前提: 1.只有在python2中才分新式类和经典类,python3中统一都是新式类 ...

  2. UVa 815 洪水!

    https://vjudge.net/problem/UVA-815 题意:一个n*m的方格区域,共有n*m个方格,每个方格是边长为10米的正方形,整个区域的外围是无限高的高墙,给出这n*m个方格的初 ...

  3. UVa 725 Division (枚举)

    题意 : 输入正整数n,按从小到大的顺序输出所有形如abcde/fghij = n的表达式,其中a-j恰好为数字0-9的一个排列(可以有前导0),2≤n≤79. 分析 : 最暴力的方法莫过于采用数组存 ...

  4. HDU 6113 度度熊的01世界【DFS/Flood Fill】

    度度熊的01世界 Accepts: 967 Submissions: 3064 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/3 ...

  5. zoj 3859 DoIt is Being Flooded (MFSet && Flood Fill)

    ZOJ :: Problems :: Show Problem 这题开始的时候想不到怎么调整每个grid的实际淹没时间,于是只好找了下watashi的题解,发现这个操作还是挺简单的. ZOJ3354 ...

  6. 图像处理之泛洪填充算法(Flood Fill Algorithm)

    泛洪填充算法(Flood Fill Algorithm) 泛洪填充算法又称洪水填充算法是在很多图形绘制软件中常用的填充算法,最熟悉不过就是 windows paint的油漆桶功能.算法的原理很简单,就 ...

  7. 图像处理------泛洪填充算法(Flood Fill Algorithm) 油漆桶功能

    泛洪填充算法(Flood Fill Algorithm) 泛洪填充算法又称洪水填充算法是在很多图形绘制软件中常用的填充算法,最熟悉不过就是 windows paint的油漆桶功能.算法的原理很简单,就 ...

  8. [LeetCode] Flood Fill 洪水填充

    An image is represented by a 2-D array of integers, each integer representing the pixel value of the ...

  9. [Swift]LeetCode733. 图像渲染 | Flood Fill

    An image is represented by a 2-D array of integers, each integer representing the pixel value of the ...

随机推荐

  1. java开发岗位面试整理

    一.Java基础 1. String类为什么是final的 2. HashMap的源码,实现原理,底层结构. 3. 说说你知道的几个Java集合类:list.set.queue.map实现类. 4. ...

  2. LUOGU P2587 [ZJOI2008]泡泡堂

    传送门 解题思路 刚开始先写了个田忌赛马的贪心,就是要是打不过就拿最弱的送死,30分...后来瞄了眼题解,发现这样是错的,比如说这样的数据 : 3 3 2 3 1 如果用田忌赛马的话,让2-3 3-1 ...

  3. HR招聘_(九)_招聘方法论(面试环节·薪资谈判和心理把控)

    .薪资谈判 薪资谈判在整个过程中非常重要,如果这一环出现问题前期的所有付出都功亏一篑,无法达成招聘目标. 谈判过程中需要遵循以下原则: 明确 通过面试后需要再次确认候选人的目前薪资和期望,虽然第一次电 ...

  4. php 简单加密解密

    <?php namespace App\Service; /* * @link http://kodcloud.com/ * @author warlee | e-mail:kodcloud@q ...

  5. JavaScript异步

    JavaScript异步类型 延迟类型:setTimeout.setInterval.setImmediate 监听事件:监听new Image加载状态.监听script加载状态.监听iframe加载 ...

  6. ie8或9下ajax跨域问题

    ie8或9下ajax跨域支持,添加如下代码 <!--[if (IE 8)|(IE 9)]><script src="https://cdn.bootcss.com/jque ...

  7. SQL SERVER 2008 R2 插入数据非常慢

    表是5字段int类型,第一个字段是主健,自增字段 表结构: id int  Uncheckedbillno bigint  Uncheckedopid int  Checkedbillopid int ...

  8. CentOS 上MySQL报错Can't connect to local Mysql server through socket '/tmp/mysql.scok' (111)

    好吧,这是最常见的MySQL服务没有打开 那就赶紧去打开啊! 在管理员模式下运行以下语句: /usr/local/mysql/bin/mysqld_safe --user=mysql & 成功 ...

  9. Linux终端常用命令(一)

    基本操作 展示全部的环境变量 export 搜索可执行文件.源文件 whereis ls 在环境变量中搜索可执行文件,并打印完整路径 which ls 展示用户命令,系统调用.库函数等 whatis ...

  10. 小爬爬4:selenium操作

    1.selenium是什么? selenium: - 概念:是一个基于浏览器自动化的模块. - 和爬虫之间的关联? - 帮我我们便捷的爬取到页面中动态加载出来的数据 - 实现模拟登陆 - 基本使用流程 ...