POJ2227(优先队列)

本文介绍了一种计算特殊形状容器(碗状)能容纳多少液体的算法。通过使用优先队列并结合邻接点的高度判断,实现了准确计算碗状容器的容量。示例输入输出帮助理解算法工作流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 
先将边界加入优先队列,每次取高度最小的点,找与其相邻且未访问过的点,若邻点高度大于等于它,直接加入优先队列更新边界,否则更新答案,并将邻点的高度置为该点高度,然后加入优先队列更新边界。
 1 //2017-08-17
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <iostream>
 5 #include <algorithm>
 6 #include <queue>
 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<Node> 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值