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. [jnhs]全套CRC校验 算法

    摘自 https://blog.csdn.net/cp1300/article/details/51443350 uint8_t crc4_itu(uint8_t *data, uint_len le ...

  2. Linux环境变量的种类

    按环境变量的生存周期来划分,Linux的环境变量可分为两类: 1永久的:需要修改配置文件,变量永久生效. 2临时的:使用export命令行声明即可,变量在关闭shell时失效.

  3. 集训队日常训练20180525-DIV2

    A.2295 求有多少素数对和等于n. 暴力. #include <bits/stdc++.h> using namespace std; int ss(int n) { ,a=sqrt( ...

  4. 2016计蒜之道复赛A 百度地图的实时路况

    百度地图的实时路况功能相当强大,能方便出行的人们避开拥堵路段.一个地区的交通便捷程度就决定了该地区的拥堵情况.假设一个地区有 nnn 个观测点,编号从 111 到 nnn.定义 d(u,v,w)d(u ...

  5. composer本地安装文档 - CSDN博客

    1.下载下图2个文件 2.将上图2个文件放到php根目录下与php.exe再同一目录 3.在composer.bat写 4.配置环境变量(将php目录复制到环境变量里) 5.将php.ini配置文件的 ...

  6. 模拟3题解 T3建造游乐园

    T3建造游乐园 这题的关键是推式子 i个点中,有g[i]个方案是度为偶数但不一定连通那么就要减去不合法的设已有j个合法,其个数为f[j],剩下i-j个的方案数是g[i-j]选出来一个固定的点在合法的j ...

  7. Linux iptables开放特定端口

    如果系统已安装则调过安装步骤 查找安装包 yum list | grep iptables 安装iptables yum install iptables-services 重启防火墙使配置文件生效 ...

  8. Excel按照某一列的重复数据设置隔行变颜色效果

    问题:如图所示,想按照A列中的重复数据设置隔重复行变颜色的效果,能否通过条件格式命令实现. 方法1:(最佳答案) 条件格式公式:=MOD(SUMPRODUCT(--($A$1:$A1<>$ ...

  9. 强力Django+杀手级xadmin开发在线教育网站

    强力Django+杀手级xadmin开发在线教育网站采用 Python3.7全新开发 整个课程都看完了,这个课程的分享可以往下看,下面有链接,之前做java开发也做了一些年头,也分享下自己看这个视频的 ...

  10. 使用 Apachetop 实时监测web服务器运行状况

    转自 http://42.96.169.71/blog/2013/01/26/shi-yong-apachetop-shi-shi-jian-ce-webfu-wu-qi-yun-xing-zhuan ...