結果

問題 No.2328 Build Walls
ユーザー KudeKude
提出日時 2023-05-28 14:19:56
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 304 ms / 3,000 ms
コード長 2,553 bytes
コンパイル時間 2,558 ms
コンパイル使用メモリ 235,788 KB
実行使用メモリ 75,084 KB
最終ジャッジ日時 2023-08-27 09:31:34
合計ジャッジ時間 7,476 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 68 ms
20,472 KB
testcase_14 AC 82 ms
20,596 KB
testcase_15 AC 68 ms
15,592 KB
testcase_16 AC 12 ms
5,428 KB
testcase_17 AC 68 ms
15,664 KB
testcase_18 AC 4 ms
4,376 KB
testcase_19 AC 8 ms
5,072 KB
testcase_20 AC 7 ms
4,380 KB
testcase_21 AC 33 ms
14,452 KB
testcase_22 AC 125 ms
29,596 KB
testcase_23 AC 265 ms
51,348 KB
testcase_24 AC 247 ms
47,684 KB
testcase_25 AC 261 ms
50,764 KB
testcase_26 AC 205 ms
40,796 KB
testcase_27 AC 237 ms
45,620 KB
testcase_28 AC 53 ms
25,688 KB
testcase_29 AC 261 ms
51,460 KB
testcase_30 AC 121 ms
35,672 KB
testcase_31 AC 105 ms
33,040 KB
testcase_32 AC 235 ms
45,116 KB
testcase_33 AC 304 ms
75,084 KB
testcase_34 AC 128 ms
36,760 KB
testcase_35 AC 282 ms
62,648 KB
testcase_36 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
namespace {
#pragma GCC diagnostic ignored "-Wunused-function"
#include<atcoder/all>
#pragma GCC diagnostic warning "-Wunused-function"
using namespace std;
using namespace atcoder;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }
template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; }
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;

const unsigned long long INF = 1002003004005006007;
template<class T>
std::vector<unsigned long long> dijkstra(std::vector<std::vector<std::pair<int,T>>>& to, int s=0) {
    struct QueElem {
        int v;
        unsigned long long c;
        bool operator<(const QueElem a) const {return c > a.c;}
        QueElem(int v, unsigned long long c): v(v), c(c) {}
    };
    std::priority_queue<QueElem> q;
    std::vector<unsigned long long> dist(to.size(), INF);
    dist[s] = 0;
    q.emplace(s, 0);
    while(!q.empty()) {
        QueElem qe = q.top(); q.pop();
        int u = qe.v;
        unsigned long long c = qe.c;
        if (c > dist[u]) continue;
        for(auto vc: to[u]) {
            int v = vc.first;
            unsigned long long nc = c + vc.second;
            if (nc < dist[v]) {
                dist[v] = nc;
                q.emplace(v, nc);
            }
        }
    }
    return dist;
}



} int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int h, w;
  cin >> h >> w;
  VVI a(h, VI(w, -1));
  rep(i, h) if (1 <= i && i < h - 1) rep(j, w) cin >> a[i][j];
  int s = h * w, t = s + 1;
  vector<vector<P>> to(t + 1);
  rep(i, h) if (a[i][0] != -1) {
    int v = i * w;
    to[s].emplace_back(v, a[i][0]);
  }
  rep(i, h) if (a[i][w - 1] != -1) {
    int v = i * w + w - 1;
    to[v].emplace_back(t, 0);
  }
  rep(i, h) rep(j, w) if (a[i][j] != -1) {
    int u = i * w + j;
    for(int di = -1; di <= 1; di++) {
      for(int dj = -1; dj <= 1; dj++) {
        if (di == 0 && dj == 0) continue;
        int ni = i + di, nj = j + dj;
        if (!(0 <= ni && ni < h && 0 <= nj && nj < w) || a[ni][nj] == -1) continue;
        int v = ni * w + nj;
        to[u].emplace_back(v, a[ni][nj]);
      }
    }
  }
  ll ans = dijkstra(to, s)[t];
  if (ans == INF) ans = -1;
  cout << ans << '\n';
}
0