結果

問題 No.2328 Build Walls
ユーザー shop_oneshop_one
提出日時 2023-05-28 15:39:12
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 157 ms / 3,000 ms
コード長 1,604 bytes
コンパイル時間 1,194 ms
コンパイル使用メモリ 121,372 KB
実行使用メモリ 13,212 KB
最終ジャッジ日時 2023-08-27 12:37:14
合計ジャッジ時間 5,009 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 31 ms
8,256 KB
testcase_14 AC 46 ms
5,568 KB
testcase_15 AC 40 ms
5,688 KB
testcase_16 AC 9 ms
4,380 KB
testcase_17 AC 42 ms
6,604 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 5 ms
4,380 KB
testcase_20 AC 5 ms
4,384 KB
testcase_21 AC 26 ms
8,004 KB
testcase_22 AC 67 ms
7,024 KB
testcase_23 AC 157 ms
13,096 KB
testcase_24 AC 150 ms
12,960 KB
testcase_25 AC 155 ms
12,960 KB
testcase_26 AC 124 ms
13,112 KB
testcase_27 AC 142 ms
13,012 KB
testcase_28 AC 50 ms
13,020 KB
testcase_29 AC 156 ms
13,040 KB
testcase_30 AC 56 ms
13,100 KB
testcase_31 AC 56 ms
12,960 KB
testcase_32 AC 141 ms
12,976 KB
testcase_33 AC 153 ms
13,212 KB
testcase_34 AC 58 ms
12,976 KB
testcase_35 AC 156 ms
13,020 KB
testcase_36 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// I SELL YOU...! 
#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
#include<queue>
#include<chrono>
#include<iomanip>
#include<map>
#include<set>
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
using TP = tuple<ll,ll,ll>;
void init_io(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(18);
}
const ll INF = 1e12;
signed main(){
  init_io();
  ll h,w;
  cin >> h >> w;
  vector<vector<ll>> a(h, vector<ll>(w, -1));
  vector<vector<ll>> dp(h, vector<ll>(w, INF));
  priority_queue<TP, vector<TP>, greater<TP>> que;
  for(int i=1;i<h-1;i++){
    for(int j=0;j<w;j++) {
      cin >> a[i][j];
      if (j==0 && a[i][j] != -1) {
        dp[i][j] = a[i][j];
        que.push(TP(a[i][j], i, j));
      }
    }
  }
  while(!que.empty()) {
    auto [v, i, j] = que.top(); que.pop();
    if (dp[i][j] < v) continue;
    ll nx[] = {i-1, i, i+1, i-1, i+1, i-1, i, i+1};
    ll ny[] = {j-1, j-1, j-1, j, j, j+1, j+1, j+1};
    for (int t=0;t<8;t++) {
      ll ni = nx[t];
      ll nj = ny[t];
      if (ni>0 && ni <h-1 && nj >= 0 && nj < w && a[ni][nj] != -1) {
        ll nv = v + a[ni][nj];
        if (dp[ni][nj] > nv) {
          que.push(TP(nv, ni, nj));
          dp[ni][nj] = nv;
        }
      }
    }
  }
  ll ans = INF;
  for (int i=0;i<h;i++) {
    ans = min(ans, dp[i][w-1]);
  }
  /*
  cout << endl;
  for (int i=0;i<h;i++) {
    for(int j=0;j<w;j++) {
      if (dp[i][j] == INF) dp[i][j] = -1;
      cout << dp[i][j] <<" ";
    }
    cout << endl;
  }
  cout << endl;
  */
  if (ans == INF) ans = -1;
  cout << ans << endl;
}
0