結果
| 問題 | No.1283 Extra Fee | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2020-11-15 14:38:52 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 270 ms / 2,000 ms | 
| コード長 | 1,281 bytes | 
| コンパイル時間 | 2,527 ms | 
| コンパイル使用メモリ | 214,532 KB | 
| 最終ジャッジ日時 | 2025-01-16 00:56:03 | 
| ジャッジサーバーID (参考情報) | judge5 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 30 | 
ソースコード
#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);
        }
      }
    }
  }
  int64_t res = min(dis[N - 1][N - 1][0], dis[N - 1][N - 1][1]);
  cout << res << endl;
}
            
            
            
        