Zoj 2849 attack of panda virus (priority_queue)

Source: Internet
Author: User

Priority queue. It is said that the flag is a parallel query set. No idea. It seems that the priority queue is directly written in STL and forced me to use STL. Prioriry_queue is unfamiliar.

PS: the smaller the value, the higher the priority. Therefore, when the <operator is overloaded, It is sorted by priority from large to small.

bool operator < (const node a, const node b) {
if(a.day != b.day) return a.day > b.day;
return a.type > b.type;
}

 

Refer to online code

View code

 1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 #include <queue>
5
6 using namespace std;
7
8 const int N = 510;
9 const int inf = ~0u>>2;
10
11 struct node {
12 int x, y;
13 int day;
14 int type;
15
16 };
17
18 priority_queue<node> q;
19
20 int dir[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
21 int m, n;
22 int mp[N][N];
23 int list[N*N];
24
25 bool operator < (const node a, const node b) {
26 if(a.day != b.day) return a.day > b.day;
27 return a.type > b.type;
28 }
29
30 bool insq(int x, int y) {
31 if(x < 0 || x >= m || y < 0 || y >= n) return false;
32 return true;
33 }
34
35 void bfs() {
36 node u, v;
37 int d, i;
38
39 while(!q.empty()) {
40 u = q.top(); q.pop();
41
42 d = -inf;
43 v.day = u.day;
44 v.type = u.type;
45
46 for(i = 0; i < 4; ++i) {
47 v.x = u.x + dir[i][0];
48 v.y = u.y + dir[i][1];
49
50 if(!insq(v.x, v.y) || mp[v.x][v.y] > 0) continue;
51
52 if(-1*mp[v.x][v.y] <= v.day) {
53 mp[v.x][v.y] = v.type;
54 list[v.type]++;
55 q.push(v);
56 } else if(mp[v.x][v.y] > d) {
57 d = mp[v.x][v.y];
58 }
59 }
60 if(d != -inf) {
61 u.day = -1*d;
62 q.push(u);
63 }
64 }
65 }
66
67 int main() {
68 freopen("data.in", "r", stdin);
69
70 int i, j, t;
71 node p;
72 while(~scanf("%d%d", &m, &n)) {
73 memset(list, 0, sizeof(list));
74
75 for(i = 0; i < m; ++i) {
76 for(j = 0; j < n; ++j) {
77 scanf("%d", &mp[i][j]);
78 if(mp[i][j] > 0) {
79 p.x = i; p.y = j;
80 p.day = 1; p.type = mp[i][j];
81 list[p.type] ++;
82 q.push(p);
83 }
84 }
85 }
86 bfs();
87 scanf("%d", &t);
88 while(t--) {
89 scanf("%d", &i);
90 printf("%d\n", list[i]);
91 }
92 }
93 return 0;
94 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.