結果

問題 No.2328 Build Walls
ユーザー shirokamishirokami
提出日時 2023-05-28 16:19:31
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 162 ms / 3,000 ms
コード長 3,347 bytes
コンパイル時間 4,717 ms
コンパイル使用メモリ 329,364 KB
実行使用メモリ 18,388 KB
最終ジャッジ日時 2023-08-27 13:53:11
合計ジャッジ時間 7,890 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 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 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 31 ms
11,260 KB
testcase_14 AC 43 ms
7,184 KB
testcase_15 AC 33 ms
7,028 KB
testcase_16 AC 7 ms
4,380 KB
testcase_17 AC 35 ms
8,032 KB
testcase_18 AC 3 ms
4,376 KB
testcase_19 AC 5 ms
4,380 KB
testcase_20 AC 4 ms
4,376 KB
testcase_21 AC 26 ms
10,448 KB
testcase_22 AC 66 ms
8,952 KB
testcase_23 AC 136 ms
18,224 KB
testcase_24 AC 128 ms
18,216 KB
testcase_25 AC 134 ms
18,292 KB
testcase_26 AC 110 ms
18,340 KB
testcase_27 AC 120 ms
18,372 KB
testcase_28 AC 52 ms
18,328 KB
testcase_29 AC 134 ms
18,196 KB
testcase_30 AC 58 ms
18,388 KB
testcase_31 AC 56 ms
18,160 KB
testcase_32 AC 121 ms
18,328 KB
testcase_33 AC 162 ms
18,328 KB
testcase_34 AC 58 ms
18,252 KB
testcase_35 AC 149 ms
18,188 KB
testcase_36 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/extc++.h>
using namespace std;
// using namespace __gnu_pbds;
// #include <boost/multiprecision/cpp_int.hpp>
// using Bint = boost::multiprecision::cpp_int;
// #include <atcoder/all>
// using namespace atcoder;
// https://atcoder.github.io/ac-library/production/document_ja/
typedef long long int ll;
typedef long double ld;
constexpr ll mod = 1e9+7;
constexpr ll INF = 9'223'372'036'854'775'807/10;
#define rep(i,n) for (ll i = 0; i < ll(n); ++i)
#define Rep(i,a,n) for (ll i = (a); i < ll(n); ++i)
#define All(a) (a).begin(),(a).end()
#define Pi acos(-1)
using V = vector<ll>;
using P = pair<ll,ll>;
vector<ll> dx = {1, 0, -1, 0, 1, 1, -1, -1};
vector<ll> dy = {0, 1, 0, -1, 1, -1, 1, -1};
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
struct Edge{ll to, cost;};
using Graph = vector<vector<Edge>>;
struct IoSetup {
  IoSetup() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << setprecision(15) << fixed;
  }
} iosetup;
void print(vector<string> &v) {
  for (string s : v) {
    cout << s << '\n';
  }
}
template<typename T>
void print(vector<T> &v, int w = 0) {
  for (int i = 0; i < (int)v.size(); i++) {
    cout << right << setw(w) << v[i] << " \n"[i == (int)v.size() - 1];
  }
}
template<typename T>
void print(vector<vector<T>> &v, int w = 0) {
  for (int i = 0; i < (int)v.size(); i++) {
    print(v[i], w);
  }
}
template<typename T>
void print(const T& arg) {
  cout << arg << '\n';
}
template<typename T, typename... Args>
void print(const T& arg, const Args&... args) {
  cout << arg << ' ';
  print(args...);
}

vector<vector<long long>> grid_dijkstra(vector<vector<long long>> grid) {
  long long H = grid.size();
  long long W = grid[0].size();
  vector<vector<long long>> dist(H, vector<long long>(W, INF));
  priority_queue<pair<long long, pair<long long, long long>>, vector<pair<long long, pair<long long, long long>>>, greater<pair<long long, pair<long long, long long>>>> pq;
  rep(i,H) {
    pq.push({grid[i][0], {i, 0}});
    dist[i][0] = grid[i][0];
  }
  while(!pq.empty()) {
    pair<long long, pair<long long, long long>> p = pq.top();
    pq.pop();
    long long y = p.second.first;
    long long x = p.second.second;
    rep (i, 8) {
      long long ny = y + dy[i];
      long long nx = x + dx[i];
      if (ny < 0 || ny >= H || nx < 0 || nx >= W) continue; // グリッドの外に出たらcontinue
      if (dist[ny][nx] > dist[y][x] + grid[ny][nx]) { // 距離が更新できるなら更新
        dist[ny][nx] = dist[y][x] + grid[ny][nx];
        pq.push(pair<long long, pair<long long, long long>>(dist[ny][nx], P(ny, nx))); 
      }
    } 
  }
  return dist;
}

/*
使い方
dijkstra(grid, sy, sx); {sy, sx} から各頂点へダイクストラ

sy: スタートy座標 (縦)
sx: スタートx座標 (横)
grid: コスト

vector<vector<long long>> dist = grid_dijkstra(sy, sx, grid);
*/

int main() {
  ll h, w;
  cin >> h >> w;
  h -= 2;
  vector<vector<ll>> grid(h, vector<ll>(w));
  rep(i,h) {
    rep(j,w) {
      cin >> grid[i][j];
      if (grid[i][j] == -1) grid[i][j] = INF;
    }
  }
  ll ans = INF;
  auto dist = grid_dijkstra(grid);
  rep(i,h) {
    chmin(ans, dist[i][w-1]);
  }
  if (ans == INF) print(-1);
  else print(ans);
}
0