이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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);
    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... |