結果

問題 No.2328 Build Walls
ユーザー tnakao0123tnakao0123
提出日時 2023-06-26 18:12:43
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 304 ms / 3,000 ms
コード長 1,700 bytes
コンパイル時間 516 ms
コンパイル使用メモリ 58,624 KB
実行使用メモリ 72,344 KB
最終ジャッジ日時 2023-09-16 12:30:33
合計ジャッジ時間 5,410 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
21,308 KB
testcase_01 AC 7 ms
21,404 KB
testcase_02 AC 7 ms
21,368 KB
testcase_03 AC 8 ms
21,404 KB
testcase_04 AC 9 ms
21,344 KB
testcase_05 AC 8 ms
21,448 KB
testcase_06 AC 8 ms
21,352 KB
testcase_07 AC 8 ms
21,428 KB
testcase_08 AC 8 ms
21,360 KB
testcase_09 AC 8 ms
21,344 KB
testcase_10 AC 8 ms
21,416 KB
testcase_11 AC 8 ms
21,420 KB
testcase_12 AC 8 ms
21,420 KB
testcase_13 AC 68 ms
27,952 KB
testcase_14 AC 85 ms
33,584 KB
testcase_15 AC 69 ms
28,904 KB
testcase_16 AC 18 ms
22,440 KB
testcase_17 AC 70 ms
26,892 KB
testcase_18 AC 10 ms
21,512 KB
testcase_19 AC 14 ms
21,676 KB
testcase_20 AC 13 ms
21,996 KB
testcase_21 AC 37 ms
23,208 KB
testcase_22 AC 127 ms
40,664 KB
testcase_23 AC 258 ms
48,688 KB
testcase_24 AC 242 ms
45,048 KB
testcase_25 AC 254 ms
48,040 KB
testcase_26 AC 196 ms
38,116 KB
testcase_27 AC 230 ms
43,036 KB
testcase_28 AC 58 ms
23,120 KB
testcase_29 AC 257 ms
48,776 KB
testcase_30 AC 117 ms
33,228 KB
testcase_31 AC 102 ms
30,560 KB
testcase_32 AC 225 ms
42,332 KB
testcase_33 AC 304 ms
72,344 KB
testcase_34 AC 123 ms
34,240 KB
testcase_35 AC 285 ms
60,144 KB
testcase_36 AC 8 ms
21,364 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 2328.cc:  No.2328 Build Walls - yukicoder
 */

#include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>
 
using namespace std;

/* constant */

const int MAX_H = 800;
const int MAX_W = 800;
const int MAX_N = MAX_H * MAX_W + 2;
const int INF = 1 << 30;

const int dxs[] = { 1, 1, 0, -1, -1, -1, 0, 1 };
const int dys[] = { 0, -1, -1, -1, 0, 1, 1, 1 };

/* typedef */

typedef pair<int,int> pii;
typedef vector<pii> vpii;

/* global variables */

int as[MAX_H][MAX_W], ds[MAX_N];
vpii nbrs[MAX_N];

/* subroutines */

/* main */

int main() {
  int h, w;
  scanf("%d%d", &h, &w);
  h -= 2;

  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) scanf("%d", as[i] + j);

  int st = h * w, gl = st + 1, n = gl + 1;

  for (int y = 0, u = 0; y < h; y++)
    for (int x = 0; x < w; x++, u++)
      if (as[y][x] >= 0)
	for (int di = 0; di < 8; di++) {
	  int vy = y + dys[di], vx = x + dxs[di], v = vy * w + vx;
	  if (vy >= 0 && vy < h && vx >= 0 && vx < w && as[vy][vx] >= 0)
	    nbrs[u].push_back(pii(v, as[vy][vx]));
	}

  for (int y = 0; y < h; y++) {
    if (as[y][0] >= 0) nbrs[st].push_back(pii(y * w, as[y][0]));
    if (as[y][w - 1] >= 0) nbrs[y * w + (w - 1)].push_back(pii(gl, 0));
  }

  fill(ds, ds + n, INF);
  ds[st] = 0;
  priority_queue<pii> q;
  q.push(pii(0, st));

  while (! q.empty()) {
    auto ue = q.top(); q.pop();
    int ud = -ue.first, u = ue.second;
    if (ds[u] != ud) continue;
    if (u == gl) break;

    for (auto &vw: nbrs[u]) {
      int v = vw.first, vd = ud + vw.second;
      if (ds[v] > vd) {
	ds[v] = vd;
	q.push(pii(-vd, v));
      }
    }
  }
  
  printf("%d\n", (ds[gl] < INF) ? ds[gl] : -1);
  return 0;
}
0