結果

問題 No.1283 Extra Fee
ユーザー tonyu0tonyu0
提出日時 2020-11-07 16:26:02
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 1,653 bytes
コンパイル時間 1,054 ms
コンパイル使用メモリ 115,888 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-29 20:37:31
合計ジャッジ時間 5,193 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#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];
ll dp[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;
    // b == 0かつ料金じょふぁあればb == 1にも遷移する

    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) {
        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;
}
0