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)
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 #include3 #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 }