The Wedding Juicer

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 3440   Accepted: 1551

Description

Farmer John's cows have taken a side job designing interesting punch-bowl designs. The designs are created as follows:

      * A flat board of size W cm x H cm is procured (3 <= W <= 300, 3 <= H <= 300)
    * On every 1 cm x 1 cm square of the board, a 1 cm x 1 cm block is placed. This block has some integer height B (1 <= B <= 1,000,000,000)

The blocks are all glued together carefully so that punch will not drain through them. They are glued so well, in fact, that the corner blocks really don't matter!

FJ's cows can never figure out, however, just how much punch their bowl designs will hold. Presuming the bowl is freestanding (i.e., no special walls around the bowl), calculate how much juice the bowl can hold. Some juice bowls, of course, leak out all the juice on the edges and will hold 0.

Input

* Line 1: Two space-separated integers, W and H

* Lines 2..H+1: Line i+1 contains row i of bowl heights: W space-separated integers each of which represents the height B of a square in the bowl. The first integer is the height of column 1, the second integers is the height of column 2, and so on.

Output

* Line 1: A single integer that is the number of cc's the described bowl will hold.

Sample Input

4 5
5 8 7 7
5 2 1 5
7 1 7 1
8 9 6 9
9 8 9 9

Sample Output

12

Hint

OUTPUT DETAILS:

Fill-up the two squares of height 1 to height 5, for 4 cc for each square. Fill the square of height 2 to height 5, for 3 cc of joice. Fill the square of height 6 to height 7 for 1 cc of juice. 2*4 + 3 + 1 = 12.

Source

 
先将边界加入优先队列,每次取高度最小的点,找与其相邻且未访问过的点,若邻点高度大于等于它,直接加入优先队列更新边界,否则更新答案,并将邻点的高度置为该点高度,然后加入优先队列更新边界。
 //2017-08-17
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue> using namespace std; const int N = ;
int G[N][N], vis[N][N], n, m;
int dx[] = {, , , -};
int dy[] = {, , -, };
struct Node{
int x, y, h;
bool operator<(const Node X) const{
return h > X.h;
}
Node(int _x, int _y, int _h):x(_x), y(_y), h(_h){}
};
priority_queue<Node> pq; void work(){
int ans = , cnt = ;
while(!pq.empty()){
Node node = pq.top();
pq.pop();
for(int i = ; i < ; i++){
int nx = node.x + dx[i];
int ny = node.y + dy[i];
if(nx< || nx>=n || ny< || ny>=m || vis[nx][ny])continue;
vis[nx][ny] = ;
if(G[nx][ny] >= node.h){
Node tmp(nx, ny, G[nx][ny]);
pq.push(tmp);
}else{
ans += node.h-G[nx][ny];
Node tmp(nx, ny, node.h);
pq.push(tmp);
}
cnt++;
}
if(cnt >= (n-)*(m-))break;
}
printf("%d\n", ans);
} int main()
{
//freopen("input2227.txt", "r", stdin);
while(scanf("%d%d", &m, &n) != EOF){
while(!pq.empty())pq.pop();
memset(vis, , sizeof(vis));
for(int i = ; i < n; i++)
for(int j = ; j < m; j++){
scanf("%d", &G[i][j]);
if(i == || i == n- || j == || j == m-){
Node node(i, j, G[i][j]);
pq.push(node);
vis[i][j] = ;
}
}
work();
} return ;
}

POJ2227(优先队列)的更多相关文章

  1. 堆排序与优先队列——算法导论(7)

    1. 预备知识 (1) 基本概念     如图,(二叉)堆是一个数组,它可以被看成一个近似的完全二叉树.树中的每一个结点对应数组中的一个元素.除了最底层外,该树是完全充满的,而且从左向右填充.堆的数组 ...

  2. 数据结构:优先队列 基于list实现(python版)

    #!/usr/bin/env python # -*- coding:utf-8 -*- #Author: Minion-Xu #list实现优先队列 class ListPriQueueValueE ...

  3. python优先队列,队列和栈

    打印列表的疑问 class Node: def __str__(self): return "haha" print([Node(),Node()]) print(Node()) ...

  4. 数据结构作业——Sanji(优先队列)

    山治的婚约 Description 我们知道,山治原来是地下有名的杀人家族文斯莫克家族的三子,目前山治的弟弟已经出现,叫做四治,大哥二哥就叫汪(One)治跟突(Two)治好了(跟本剧情无关) .山治知 ...

  5. Java优先队列

    按照Java api的说法: java.util.PriorityQueue.PriorityQueue() Creates a PriorityQueue with the default init ...

  6. 优先队列实现Huffman编码

    首先把所有的字符加入到优先队列,然后每次弹出两个结点,用这两个结点作为左右孩子,构造一个子树,子树的跟结点的权值为左右孩子的权值的和,然后将子树插入到优先队列,重复这个步骤,直到优先队列中只有一个结点 ...

  7. “玲珑杯”ACM比赛 Round #7 B -- Capture(并查集+优先队列)

    题意:初始时有个首都1,有n个操作 +V表示有一个新的城市连接到了V号城市 -V表示V号城市断开了连接,同时V的子城市也会断开连接 每次输出在每次操作后到首都1距离最远的城市编号,多个距离相同输出编号 ...

  8. Dijkstra算法优先队列实现与Bellman_Ford队列实现的理解

    /* Dijkstra算法用优先队列来实现,实现了每一条边最多遍历一次. 要知道,我们从队列头部找到的都是到 已经"建好树"的最短距离以及该节点编号, 并由该节点去更新 树根 到其 ...

  9. 数据结构作业——ギリギリ eye(贪心+优先队列/贪心+并查集)

    ギリギリ eye Description A.D.1999,由坠落地球的“谜之战舰”带来的 Over Technology,揭示了人类历史和远古文明之间的丝丝联系, 促使人类终止彼此间的战争,一方面面 ...

随机推荐

  1. 《Linux-基础篇笔记》 Vim编辑器(二)

    Linux图形化界面下的文本编辑器 gedit . libre office . evince PDF阅读器 ①gedit是一个GNOME桌面环境下兼容UTF-8的文本编辑器.它使用GTK+编写而成, ...

  2. 23_pikle/shevel/json

    一.序列化       存储数据或者传输数据时,需要把对象进行处理,把对象处理成方便存储和传输的数据格式.不同的序列化,结果也不同.     序列化方式:         (1) pickle 可以将 ...

  3. 基于alpine用dockerfile创建的爬虫Scrapy镜像

    一.下载alpine镜像 [root@DockerBrian ~]# docker pull alpine Using default tag: latest Trying to pull repos ...

  4. C#简单操作MongoDB

    一 安装MongoDB 官网按需下载, 安装, 一步到位. 二 VS创建新项目 创建一个.netcore console项目, 然后nuget安装驱动MongoDB.Driver 三 建立连接 在Pr ...

  5. Java模式—简单工厂模式

    简单工厂模式:是由一个工厂对象决定创建出哪一种产品类的实例,简单工厂模式是工厂模式家族中最简单实用的模式. 目的:为创建对象提供过渡接口,以便将创建对象的具体过程屏蔽隔离起来,达到提高灵活性的目的. ...

  6. 微信小程序(wx:for)遍历对象

    最近在折腾微信小程序,遇到这么一个情况:后端返回一个key-value的对象数据,需要遍历对象的key-value,然后渲染到视图中.就像下面这样: { '2018-1-9':{ address: ' ...

  7. 【原创】基于Bootstrap的Modal二次封装

    前言 Bootstrap:Twitter推出的一个开源的用于前端开发的工具包.它由Twitter的设计师Mark Otto和Jacob Thornton合作开发,是一个CSS/HTML框架 官方网站: ...

  8. Django开发密码管理表实例【附源码】

    文章及代码比较基础,适合初.中级人员,高手略过 阅读此篇文章你可以: 获取一个Django实现增删改查的案例源码 了解数据加密的使用场景和方法以及如何在Python3中使用 背景介绍 DBA需要维护一 ...

  9. Spring-mvc登录方法及JSP的拦截

    添加登录拦截器:LoginInterceptor import javax.servlet.http.HttpServletRequest; import javax.servlet.http.Htt ...

  10. ASP.NET MVC5+EF6+LayUI实战教程,通用后台管理系统框架(4)- 漂亮的登录界面

    前言 这一讲,给大家添加登录页面 实现 添加Login的Index视图 @{ Layout = null; } <!DOCTYPE html> <html class="l ...