結果
| 問題 | No.1283 Extra Fee | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2020-11-07 16:27:10 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 999 ms / 2,000 ms | 
| コード長 | 1,612 bytes | 
| コンパイル時間 | 1,626 ms | 
| コンパイル使用メモリ | 112,656 KB | 
| 最終ジャッジ日時 | 2025-01-15 21:31:33 | 
| ジャッジサーバーID (参考情報) | judge2 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 30 | 
ソースコード
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <tuple>
#include <vector>
using namespace std;
using ll = long long;
#define rep(i, j, n) for (int i = j; i < (n); ++i)
#define rrep(i, j, n) for (int i = (n)-1; j <= i; --i)
[[maybe_unused]] constexpr ll MOD = 1000000007;
[[maybe_unused]] constexpr int INF = 0x3f3f3f3f;
[[maybe_unused]] constexpr ll INFL = 0x3f3f3f3f3f3f3f3fLL;
const int MAX = 505;
ll cost[MAX * MAX];
ll dp[MAX * MAX][2];
int main() {
  int n, m;
  cin >> n >> m;
  rep(i, 0, m) {
    int h, w, c;
    cin >> h >> w >> c;
    --w, --h;
    cost[n * h + w] = c;
  }
  rep(i, 0, n * n + 1) rep(j, 0, 2) dp[i][j] = INFL;
  dp[0][0] = 0;
  int dw[4] = {0, 0, -1, 1};
  int dh[4] = {1, -1, 0, 0};
  using ili = tuple<int, ll, int>;
  priority_queue<ili, vector<ili>, greater<ili>> pq;
  pq.emplace(0, 0, 0); // vertex, cost, bool
  while (pq.size()) {
    auto [v, c, b] = pq.top();
    pq.pop();
    if (dp[v][b] < c) continue;
    int ch = v / n;
    int cw = v % n;
    rep(i, 0, 4) {
      int nh = ch + dh[i];
      int nw = cw + dw[i];
      int nv = nh * n + nw;
      if (0 <= nh && nh < n && 0 <= nw && nw < n) {
        // normal
        if (dp[nv][b] > c + 1 + cost[nv]) {
          dp[nv][b] = c + 1 + cost[nv];
          pq.emplace(nv, dp[nv][b], b);
        }
        if (b == 0 && dp[nv][1] > c + 1) {
          dp[nv][1] = c + 1;
          pq.emplace(nv, dp[nv][1], 1);
        }
      }
    }
  }
  cout << min(dp[n * n - 1][0], dp[n * n - 1][1]) << '\n';
  return 0;
}
            
            
            
        