ZOJ 2849 Attack of Panda Virus (优先队列 priority_queue)
优先队列,据说标程是并查集,没思路。貌似优先队列都是直接用stl写的,又逼我用stl了。prioriry_queue不熟。
ps: value值越小,优先级越高。所以重载 < 运算符时按优先级从大到小排序
bool operator < (const node a, const node b) {
if(a.day != b.day) return a.day > b.day;
return a.type > b.type;
}
参考网上的代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 }