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. fedora python访问mysql

    1 下载驱动库 http://sourceforge.net/projects/mysql-python/ 2, yum install mysql-dev* yum install python-d ...

  2. 哈哈哈哈,我竟然发现了个MSDN里面的笔误

    typedef __PROCESSOR_INFO {  WORD wVersion;   WCHAR szProcessorCore[40];  WORD wCoreRevision;  WCHAR ...

  3. easyui combobox下拉框中显示大于号小于号的问题

    前两天同事做了个功能,通过勾选下拉框里的值进行列表查询,结果下拉框里的值是“0<t<=2”.“2<t<=5”.“t>5”这样的. combobox是用脚本渲染出来的,里面 ...

  4. 当移动数据分析需求遇到Quick BI

    我叫洞幺,是一名大型婚恋网站“我在这等你”的资深老员工,虽然在公司五六年,还在一线搬砖.“我在这等你”成立15年,目前积累注册用户高达2亿多,在我们网站成功牵手的用户达2千多万.目前我们的公司在CEO ...

  5. qq在线

    <a target="_blank" href="http://wpa.qq.com/msgrd?v=3&uin=1749904992&site=q ...

  6. C# 通过URL得到图片的问题

    第一个方法在读取某些图片会报错 public static Image get_Fill_image(string url) { var image = new Image(); image.Sour ...

  7. Python2.7用sys.stdout.write实现打印刷新

    如何能在控制台实现在一行中显示进度的信息呢,就像使用pip安装时的进度那样. 如果用print则会打印成多行,下面这个小技巧可以在一行中打印: import time import sys if __ ...

  8. buffer的相关小知识

    php与mysql的连接有三种方式,mysql,mysqli,pdo.不管使用哪种方式进行连接,都有使用buffer和不使用buffer的区别. 什么叫使用buffer和不使用buffer呢? 客户端 ...

  9. Spark-day01

      Spark初始 什么是Spark Apache Spark 是专为大规模数据处理而设计的快速通用的计算引擎.Spark是UC Berkeley AMP lab (加州大学伯克利分校的AMP实验室) ...

  10. poj3463&&hdu1688 次短路(dijkstra)

    A*算法超内存. 对于最短路,我们可以维护dis[]数组,来求得最短路,但是此题有次短路,所以定义dis[][2],dis[][0]表示最短路,dis[][1]表示次短路;cnt[][2],cnt[] ...