This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
typedef pair<int, int> pii;
const int maxn = 1010;
int n, m, k;
int d[maxn * maxn];
vector<pii> adj[maxn * maxn];
int get_idx(int i, int j) {
if(i == 0 || i == n+1 || j == 0 || j == m+1) return 0;
return (i - 1) * m + j;
}
int main(void) {
ios_base::sync_with_stdio(0); cin.tie(0);
memset(adj, 0x3f, sizeof(adj));
cin >> n >> m >> k;
for(int i = 1; i <= n+1; ++i) {
for(int j = 1; j <= m; ++j) {
int now; cin >> now;
now = ~now ? now : 1e9 + 7;
int i1 = get_idx(i - 1, j), i2 = get_idx(i, j);
adj[i1].pb({i2, now});
adj[i2].pb({i1, now});
}
}
for(int i = 1; i <= n; ++i) {
for(int j = 1; j <= m+1; ++j) {
int now; cin >> now;
now = ~now ? now : 1e9 + 7;
int i1 = get_idx(i, j - 1), i2 = get_idx(i, j);
adj[i1].pb({i2, now});
adj[i2].pb({i1, now});
}
}
for(int i = 1; i <= n; ++i) for(int j = 1; j <= m; ++j) d[get_idx(i, j)] = k;
priority_queue<pair<int, int>> pq;
d[0] = 0; pq.push({-0, 0});
while(!pq.empty()) {
int wei = -pq.top().first, now = pq.top().second;
pq.pop();
if(wei > d[now]) continue;
for(auto [nxt, nw] : adj[now]) {
if(max(wei, nw) < d[nxt]) {
d[nxt] = max(wei, nw);
pq.push({-d[nxt], nxt});
}
}
}
int ans = 0;
for(int i = 1; i <= n; ++i) for(int j = 1; j <= m; ++j) ans += d[get_idx(i, j)];
cout << ans;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |