結果

問題 No.1283 Extra Fee
ユーザー tonyu0tonyu0
提出日時 2020-11-07 16:27:10
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 787 ms / 2,000 ms
コード長 1,612 bytes
コンパイル時間 1,099 ms
コンパイル使用メモリ 117,552 KB
実行使用メモリ 10,884 KB
最終ジャッジ日時 2024-04-27 23:37:01
合計ジャッジ時間 9,666 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 3 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 3 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 21 ms
6,944 KB
testcase_12 AC 43 ms
6,940 KB
testcase_13 AC 19 ms
6,944 KB
testcase_14 AC 86 ms
7,984 KB
testcase_15 AC 147 ms
6,944 KB
testcase_16 AC 23 ms
6,940 KB
testcase_17 AC 195 ms
9,864 KB
testcase_18 AC 787 ms
9,900 KB
testcase_19 AC 661 ms
9,988 KB
testcase_20 AC 682 ms
10,660 KB
testcase_21 AC 630 ms
10,856 KB
testcase_22 AC 548 ms
9,700 KB
testcase_23 AC 214 ms
9,336 KB
testcase_24 AC 293 ms
9,336 KB
testcase_25 AC 615 ms
10,884 KB
testcase_26 AC 669 ms
10,884 KB
testcase_27 AC 722 ms
10,876 KB
testcase_28 AC 652 ms
10,880 KB
testcase_29 AC 209 ms
9,960 KB
権限があれば一括ダウンロードができます

ソースコード

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 * 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;
}
0