結果

問題 No.2328 Build Walls
ユーザー MMMM
提出日時 2023-06-02 00:58:46
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 284 ms / 3,000 ms
コード長 1,444 bytes
コンパイル時間 1,757 ms
コンパイル使用メモリ 178,124 KB
実行使用メモリ 13,680 KB
最終ジャッジ日時 2023-08-28 02:14:15
合計ジャッジ時間 6,496 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 66 ms
8,660 KB
testcase_14 AC 77 ms
6,044 KB
testcase_15 AC 65 ms
5,952 KB
testcase_16 AC 13 ms
4,380 KB
testcase_17 AC 69 ms
6,700 KB
testcase_18 AC 4 ms
4,384 KB
testcase_19 AC 10 ms
4,376 KB
testcase_20 AC 7 ms
4,380 KB
testcase_21 AC 57 ms
8,360 KB
testcase_22 AC 118 ms
7,232 KB
testcase_23 AC 259 ms
13,460 KB
testcase_24 AC 245 ms
13,440 KB
testcase_25 AC 259 ms
13,420 KB
testcase_26 AC 212 ms
13,592 KB
testcase_27 AC 238 ms
13,444 KB
testcase_28 AC 113 ms
13,092 KB
testcase_29 AC 258 ms
13,400 KB
testcase_30 AC 123 ms
13,572 KB
testcase_31 AC 121 ms
13,480 KB
testcase_32 AC 235 ms
13,680 KB
testcase_33 AC 284 ms
13,456 KB
testcase_34 AC 125 ms
13,432 KB
testcase_35 AC 271 ms
13,396 KB
testcase_36 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#define chmin(x,y) (x) = min((x),(y))
using namespace std;
using ll = long long;
bool debug = 0;
const vector<int> dx = {1,1,0,-1,-1,-1,0,1}, dy = {0,1,1,1,0,-1,-1,-1};

int main(){
  // input
  int H,W; cin >> H >> W;
  vector<vector<ll>> A(H,vector<ll>(W,-1));
  for (int i = 1; i + 1 < H; i++)
    for (int j = 0; j < W; j++)
      cin >> A[i][j];
  
  // solve: dijkstra
  vector<vector<ll>> dis(H,vector<ll>(W,9e18));
  priority_queue<vector<ll>> pq; // {cost,cur_x,cur_y}
  for (int i = 1; i + 1 < H; i++){
    if(A[i][0] != -1){
      pq.push({-A[i][0],i,0});
      dis[i][0] = A[i][0];
    }
  }
  
  while(!pq.empty()){
    ll cost = -pq.top()[0], cur_x = pq.top()[1], cur_y = pq.top()[2];
    pq.pop();
    // chmin(dis[cur_x][cur_y],cost);
    for(int j = 0; j < 8; j++){
      ll nxt_x = cur_x + dx[j], nxt_y = cur_y + dy[j];
      if(nxt_x < 0 || nxt_x >= H || nxt_y < 0 || nxt_y >= W) continue;
      if(A[nxt_x][nxt_y] == -1) continue;
      
      if(dis[nxt_x][nxt_y] > cost + A[nxt_x][nxt_y]){
        dis[nxt_x][nxt_y] = cost + A[nxt_x][nxt_y];
        pq.push({-(cost+A[nxt_x][nxt_y]),nxt_x,nxt_y});
        if(debug) cout << nxt_x << " " << nxt_y << " " << cost + A[nxt_x][nxt_y] << endl;
      }
    }
  }
  
  // output
  ll ans = 9e18;
  for (int i = 0; i < H; i++){
    if(debug) cout << i << ":" << dis[i][W-1] << endl;
    chmin(ans,dis[i][W-1]);
  }
  cout << (ans < 2e9? ans : -1) << endl;
}
0