結果

問題 No.2328 Build Walls
ユーザー shirokamishirokami
提出日時 2023-05-28 15:12:06
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 3,415 bytes
コンパイル時間 4,328 ms
コンパイル使用メモリ 330,800 KB
実行使用メモリ 11,804 KB
最終ジャッジ日時 2023-08-27 11:37:12
合計ジャッジ時間 10,664 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
8,756 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 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 1,036 ms
11,216 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/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(long long sy, long long sx, 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;
  pq.push({grid[sy][sx], {sy, sx}}); // スタート地点
  dist[sy][sx] = grid[sy][sx];
  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;
  rep(i,h) {
    auto dist = grid_dijkstra(i, 0, grid);
    rep(j,h) {
      chmin(ans, dist[j][w-1]);
    }
  }
  if (ans == INF) print(-1);
  else print(ans);
}
0