#include <bits/stdc++.h>
using namespace std;
int A[2020][2020], n, m, h;
int d[2010][2010], E[1010][1010][4];
int dx[] = { 1,-1,0,0 }, dy[] = { 0,0,1,-1 };
struct str {
int x, y, d;
};
priority_queue<str> pq;
bool operator<(str i, str j) {
return i.d > j.d;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m >> h;
for (int i = 1; i <= n + 1; i++) {
for (int j = 1; j <= m; j++) {
d[i][j] = h;
cin >> E[i][j][1];
E[i - 1][j][0] = E[i][j][1];
}
}
//for(int j = 1;j<=m;j++) cin >> E[n+1][j][0];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m+1; j++) {
cin >> E[i][j][3];
E[i][j - 1][2] = E[i][j][3];
}
}
//cout << '\n';
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
int edge = 1 << 30;
for (int l = 0; l < 4; l++) {
//printf("[%d] ", E[i][j][l]);
int x = i + dx[l], y = j + dy[l];
if (x == 0 || y == 0 || x == n + 1 || y == m + 1) {
if (E[i][j][l] != -1) {
edge = min(edge, E[i][j][l]);
//cout << "EDGE IS " << edge << ' ';
}
}
//else cout << x << ' ' << y << '\n';
}
if (edge != 1 << 30) {
// printf("\n(%d %d) pushed %d\n", i, j, edge);
d[i][j] = edge;
pq.push({ i,j,edge });
}
}
}
// for(int i = 0;i<=2*n;i++){
// for(int j = 0;j<=2*m;j++)
// printf("%3d ",A[i][j]);
// cout << '\n';
// }
while (!pq.empty()) {
str now = pq.top();
//printf("%d %d : %d\n", now.x, now.y, now.d);
pq.pop();
if (d[now.x][now.y] < now.d) continue;
for (int i = 0; i < 4; i++) {
if (E[now.x][now.y][i] == -1) continue;
int ax = now.x + dx[i], ay = now.y + dy[i];
if (ax<1 || ay<1 || ax>n || ay>m) continue;
int cost = max(now.d, E[now.x][now.y][i]);
if (d[ax][ay] > cost) {
//printf("(%d %d) -> (%d %d)[%d]\n", now.x, now.y, ax, ay,E[now.x][now.y][i]);
d[ax][ay] = cost;
pq.push({ ax,ay,cost });
}
}
}
long long sum = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
sum += d[i][j];
}
}
cout << sum;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
308 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
292 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
380 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
130 ms |
29804 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
124 ms |
26612 KB |
Output is correct |
4 |
Correct |
122 ms |
27212 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
193 ms |
29048 KB |
Output is correct |
7 |
Correct |
107 ms |
27232 KB |
Output is correct |
8 |
Correct |
253 ms |
34192 KB |
Output is correct |
9 |
Correct |
105 ms |
26440 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
248 ms |
31036 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
852 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
328 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
1 ms |
724 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
724 KB |
Output is correct |
11 |
Correct |
1 ms |
724 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
724 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
328 KB |
Output is correct |
16 |
Correct |
2 ms |
744 KB |
Output is correct |
17 |
Correct |
1 ms |
724 KB |
Output is correct |
18 |
Correct |
1 ms |
852 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
852 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
125 ms |
29820 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
111 ms |
28580 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
126 ms |
29876 KB |
Output is correct |
13 |
Correct |
106 ms |
26612 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
124 ms |
26940 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
118 ms |
27184 KB |
Output is correct |
20 |
Correct |
1 ms |
724 KB |
Output is correct |
21 |
Correct |
462 ms |
44152 KB |
Output is correct |
22 |
Correct |
1 ms |
724 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
724 KB |
Output is correct |
25 |
Correct |
1 ms |
724 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
210 ms |
30804 KB |
Output is correct |
29 |
Correct |
196 ms |
29008 KB |
Output is correct |
30 |
Correct |
111 ms |
27228 KB |
Output is correct |
31 |
Correct |
258 ms |
34252 KB |
Output is correct |
32 |
Correct |
124 ms |
29000 KB |
Output is correct |
33 |
Correct |
0 ms |
340 KB |
Output is correct |
34 |
Correct |
104 ms |
26380 KB |
Output is correct |
35 |
Correct |
2 ms |
724 KB |
Output is correct |
36 |
Correct |
0 ms |
332 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
252 ms |
30972 KB |
Output is correct |
39 |
Correct |
525 ms |
42164 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
852 KB |
Output is correct |
42 |
Correct |
1 ms |
732 KB |
Output is correct |
43 |
Correct |
2 ms |
772 KB |
Output is correct |
44 |
Correct |
119 ms |
28392 KB |
Output is correct |
45 |
Correct |
377 ms |
36300 KB |
Output is correct |
46 |
Correct |
0 ms |
340 KB |
Output is correct |