結果

問題 No.2328 Build Walls
ユーザー MMMM
提出日時 2023-06-02 00:56:33
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,355 bytes
コンパイル時間 1,816 ms
コンパイル使用メモリ 178,192 KB
実行使用メモリ 21,976 KB
最終ジャッジ日時 2023-08-28 02:14:07
合計ジャッジ時間 7,209 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
8,752 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,384 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 73 ms
8,996 KB
testcase_14 TLE -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
権限があれば一括ダウンロードができます

ソースコード

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});
  }
  
  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]){
        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