POJ2227(优先队列)
The Wedding Juicer
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 3440 | Accepted: 1551 |
Description
- * 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
* 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
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
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(优先队列)的更多相关文章
- 堆排序与优先队列——算法导论(7)
1. 预备知识 (1) 基本概念 如图,(二叉)堆是一个数组,它可以被看成一个近似的完全二叉树.树中的每一个结点对应数组中的一个元素.除了最底层外,该树是完全充满的,而且从左向右填充.堆的数组 ...
- 数据结构:优先队列 基于list实现(python版)
#!/usr/bin/env python # -*- coding:utf-8 -*- #Author: Minion-Xu #list实现优先队列 class ListPriQueueValueE ...
- python优先队列,队列和栈
打印列表的疑问 class Node: def __str__(self): return "haha" print([Node(),Node()]) print(Node()) ...
- 数据结构作业——Sanji(优先队列)
山治的婚约 Description 我们知道,山治原来是地下有名的杀人家族文斯莫克家族的三子,目前山治的弟弟已经出现,叫做四治,大哥二哥就叫汪(One)治跟突(Two)治好了(跟本剧情无关) .山治知 ...
- Java优先队列
按照Java api的说法: java.util.PriorityQueue.PriorityQueue() Creates a PriorityQueue with the default init ...
- 优先队列实现Huffman编码
首先把所有的字符加入到优先队列,然后每次弹出两个结点,用这两个结点作为左右孩子,构造一个子树,子树的跟结点的权值为左右孩子的权值的和,然后将子树插入到优先队列,重复这个步骤,直到优先队列中只有一个结点 ...
- “玲珑杯”ACM比赛 Round #7 B -- Capture(并查集+优先队列)
题意:初始时有个首都1,有n个操作 +V表示有一个新的城市连接到了V号城市 -V表示V号城市断开了连接,同时V的子城市也会断开连接 每次输出在每次操作后到首都1距离最远的城市编号,多个距离相同输出编号 ...
- Dijkstra算法优先队列实现与Bellman_Ford队列实现的理解
/* Dijkstra算法用优先队列来实现,实现了每一条边最多遍历一次. 要知道,我们从队列头部找到的都是到 已经"建好树"的最短距离以及该节点编号, 并由该节点去更新 树根 到其 ...
- 数据结构作业——ギリギリ eye(贪心+优先队列/贪心+并查集)
ギリギリ eye Description A.D.1999,由坠落地球的“谜之战舰”带来的 Over Technology,揭示了人类历史和远古文明之间的丝丝联系, 促使人类终止彼此间的战争,一方面面 ...
随机推荐
- C++ 设置Java 环境变量完整的例子
#include <stdio.h> #include <stdlib.h> #include <windows.h> #include <iostream& ...
- Apache VirtualHost的配置
自从电脑更换为mac后, 一直没有时间去配置php的环境.导致每次要更改php代码的时候, 都是本地更改,然后直接推送到服务器上运行 这样的开发和测试及其耗时且繁琐, 所以早上特地决定弄好mac下的p ...
- SpringCloud之Eureka集群
前面我们介绍了SpringCloud注册中心Eureka,但是存在一个单点故障的问题,一个注册中心远远不能满足实际的生产环境,现在我们介绍一下如何搭建一个Eureka集群. 一:集群环境搭建 我们先建 ...
- 交换机与VLAN
1. 交换机 1.1 定义 交换机是一种基于MAC地址识别,能完成封装转发数据帧功能的网络设备. HUB集线器是一种物理层共享设备,HUB本身不能识别MAC 地址和IP地址,当同一局域网内的A主机给B ...
- CGI + FastCGI(PHP-FPM)联系与区别 【图解 + 注释】
〇.背景 参考了几篇文章,总结成 图解 + 注释 方便以后查阅. 参考资料: 1.https://www.zhihu.com/question/19582041 2.https://segmentfa ...
- vue项目经验:图形验证码接口get请求处理
一般图形验证码处理: 直接把img标签的src指向这个接口,然后在img上绑定点击事件,点击的时候更改src的地址(在原来的接口地址后面加上随机数即可,避免缓存) <img :src=" ...
- JVM(三)JVM的ClassLoader类加载器
1.类加载的生命周期 类从被加载到虚拟机内存中开始,到卸载出内存为止,它的整个生命周期包括: (1)加载 (2)验证 (3)准备 (4)解析 (5)初始化 (6)使用 (7)卸载 一共7个阶段.其中验 ...
- 03-01:springboot 整合jsp
1.修改pom文件,添加坐标 <!-- jstl --> <dependency> <groupId>javax.servlet ...
- Android在代码中获取meta标签内容
最近写SDK需要获取<meta>标签的值,网上资料很多~分享是件好事~我很快就找到了相关资料. 下面贴上代码: ApplicationInfo appInfo = null; String ...
- 第一章-Javac编译器介绍
1.Javac概述 编译器可以将编程语言的代码转换为其他形式,如Javac,将Java语言转换为虚拟机能够识别的.class文件形式.而这种将java源代码(以.java做为文件存储格式)转换为cla ...