zoukankan      html  css  js  c++  java
  • POJ2227(优先队列)

    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
    ", 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 }
  • 相关阅读:
    gRPC详解
    vue 实现无限向上滚动
    vue 封装highcharts组件
    《数据结构与算法之美》32——分治算法
    《数据结构与算法之美》31——贪心算法
    《数据结构与算法之美》30——回溯算法
    《数据结构与算法之美》29——动态规划实战
    《数据结构与算法之美》28——动态规划理论
    《数据结构与算法之美》27——初识动态规划
    《数据结构与算法之美》26——广度优先搜索与深度优先搜索
  • 原文地址:https://www.cnblogs.com/Penn000/p/7381232.html
Copyright © 2011-2022 走看看