博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ2227(优先队列)
阅读量:5104 次
发布时间:2019-06-13

本文共 3199 字,大约阅读时间需要 10 分钟。

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 55 8 7 75 2 1 57 1 7 18 9 6 99 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

 
先将边界加入优先队列,每次取高度最小的点,找与其相邻且未访问过的点,若邻点高度大于等于它,直接加入优先队列更新边界,否则更新答案,并将邻点的高度置为该点高度,然后加入优先队列更新边界。
1 //2017-08-17 2 #include 
3 #include
4 #include
5 #include
6 #include
7 8 using namespace std; 9 10 const int N = 310;11 int G[N][N], vis[N][N], n, m;12 int dx[4] = {
0, 1, 0, -1};13 int dy[4] = {
1, 0, -1, 0};14 struct Node{15 int x, y, h;16 bool operator<(const Node X) const{17 return h > X.h;18 }19 Node(int _x, int _y, int _h):x(_x), y(_y), h(_h){}20 };21 priority_queue
pq;22 23 void work(){24 int ans = 0, cnt = 0;25 while(!pq.empty()){26 Node node = pq.top();27 pq.pop();28 for(int i = 0; i < 4; i++){29 int nx = node.x + dx[i];30 int ny = node.y + dy[i];31 if(nx<0 || nx>=n || ny<0 || ny>=m || vis[nx][ny])continue;32 vis[nx][ny] = 1;33 if(G[nx][ny] >= node.h){34 Node tmp(nx, ny, G[nx][ny]);35 pq.push(tmp);36 }else{37 ans += node.h-G[nx][ny];38 Node tmp(nx, ny, node.h);39 pq.push(tmp);40 }41 cnt++;42 }43 if(cnt >= (n-2)*(m-1))break;44 }45 printf("%d\n", ans);46 }47 48 int main()49 {50 //freopen("input2227.txt", "r", stdin);51 while(scanf("%d%d", &m, &n) != EOF){52 while(!pq.empty())pq.pop();53 memset(vis, 0, sizeof(vis));54 for(int i = 0; i < n; i++)55 for(int j = 0; j < m; j++){56 scanf("%d", &G[i][j]);57 if(i == 0 || i == n-1 || j == 0 || j == m-1){58 Node node(i, j, G[i][j]);59 pq.push(node);60 vis[i][j] = 1;61 } 62 }63 work();64 }65 66 return 0;67 }

 

转载于:https://www.cnblogs.com/Penn000/p/7381232.html

你可能感兴趣的文章
LeetCode(3) || Median of Two Sorted Arrays
查看>>
大话文本检测经典模型:EAST
查看>>
待整理
查看>>
一次动态sql查询订单数据的设计
查看>>
C# 类(10) 抽象类.
查看>>
Nginx+Keepalived 实现双击热备及负载均衡
查看>>
Vue_(组件通讯)子组件向父组件传值
查看>>
jvm参数
查看>>
6个有用的MySQL语句
查看>>
我对前端MVC的理解
查看>>
Silverlight实用窍门系列:19.Silverlight调用webservice上传多个文件【附带源码实例】...
查看>>
2016.3.31考试心得
查看>>
mmap和MappedByteBuffer
查看>>
Linux的基本操作
查看>>
转-求解最大连续子数组的算法
查看>>
对数器的使用
查看>>
OracleOraDb11g_home1TNSListener服务启动后停止,某些服务在未由其他服务或程序使用时将自己主动停止...
查看>>
Redis用户添加、分页、登录、注册、加关注案例
查看>>
练习2
查看>>
【ASP.NET】演绎GridView基本操作事件
查看>>