結果

問題 No.1283 Extra Fee
ユーザー 0w1
提出日時 2020-11-15 14:38:18
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,277 bytes
コンパイル時間 2,458 ms
コンパイル使用メモリ 216,408 KB
最終ジャッジ日時 2025-01-16 00:55:49
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 20 WA * 10
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int main() {
  ios::sync_with_stdio(false);

  int N, M; {
    cin >> N >> M;
  }

  vector<vector<int>> C(N, vector<int>(N)); {
    for (int i = 0; i < M; ++i) {
      int H, W, _C;
      cin >> H >> W >> _C;
      C[H - 1][W - 1] = _C;
    }
  }

  const int64_t INF = (int64_t) 1 << 60;

  vector<vector<vector<int64_t>>> dis(N, vector<vector<int64_t>>(N, vector<int64_t>(2, INF))); {
    set<tuple<int64_t, int, int, int>> bag;
    bag.emplace(dis[0][0][0] = 0, 0, 0, 0);
    while (bag.size()) {
      int64_t d;
      int x, y, t;
      tie(d, x, y, t) = *bag.begin(); 
      bag.erase(bag.begin());
      if (d != dis[x][y][t]) continue;
      const int dx[] = { 0, 1, 0, -1 };
      const int dy[] = { 1, 0, -1, 0 };
      for (int _ = 0; _ < 4; ++_) {
        int nx = x + dx[_];
        int ny = y + dy[_];
        if (nx < 0 || nx == N || ny < 0 || ny == N) continue;
        if (d + 1 + C[nx][ny] < dis[nx][ny][t]) {
          bag.emplace(dis[nx][ny][t] = d + 1 + C[nx][ny], nx, ny, t);
        }
        if (t == 0 && d + 1 < dis[nx][ny][1]) {
          bag.emplace(dis[nx][ny][1] = d + 1, nx, ny, 1);
        }
      }
    }
  }

  int res = min(dis[N - 1][N - 1][0], dis[N - 1][N - 1][1]);
  cout << res << endl;
}
0