結果

問題 No.2366 登校
ユーザー 👑 emthrmemthrm
提出日時 2023-06-30 22:22:30
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 435 ms / 4,000 ms
コード長 2,102 bytes
コンパイル時間 3,320 ms
コンパイル使用メモリ 266,660 KB
実行使用メモリ 28,076 KB
最終ジャッジ日時 2023-09-03 02:59:54
合計ジャッジ時間 6,470 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 106 ms
9,540 KB
testcase_13 AC 118 ms
9,720 KB
testcase_14 AC 122 ms
9,544 KB
testcase_15 AC 110 ms
9,540 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 104 ms
9,564 KB
testcase_18 AC 121 ms
9,560 KB
testcase_19 AC 112 ms
9,580 KB
testcase_20 AC 114 ms
9,556 KB
testcase_21 AC 117 ms
9,620 KB
testcase_22 AC 96 ms
9,280 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 208 ms
28,076 KB
testcase_25 AC 435 ms
27,796 KB
testcase_26 AC 426 ms
27,856 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 998244353;
// constexpr int MOD = 1000000007;
constexpr int DY4[]{1, 0, -1, 0}, DX4[]{0, -1, 0, 1};
constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1};
constexpr int DX8[]{0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U>
inline bool chmax(T& a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U>
inline bool chmin(T& a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;

int main() {
  int n, m, k, t; cin >> n >> m >> k >> t;
  const int cons = n - 1 + m - 1;
  if (t >= cons) {
    cout << 0 << '\n';
    return 0;
  }
  vector c(n, vector(m, 0)), d(n, vector(m, 0));
  while (k--) {
    int a, b; cin >> a >> b; --a; --b;
    cin >> c[a][b] >> d[a][b]; --c[a][b];
  }
  vector dp(n, vector(m, vector(cons * 3 + 1, LINF)));
  dp[0][0][cons * 2 + t] = 0;
  priority_queue<tuple<ll, int, int, int>, vector<tuple<ll, int, int, int>>, greater<tuple<ll, int, int, int>>> que;
  que.emplace(0, 0, 0, cons * 2 + t);
  while (!que.empty()) {
    const auto [dp_v, i, j, tm] = que.top(); que.pop();
    if (dp_v > dp[i][j][tm]) continue;
    if (const int ntm = min(tm + c[i][j], cons * 3); chmin(dp[i][j][ntm], dp[i][j][tm] + d[i][j])) {
      que.emplace(dp[i][j][ntm], i, j, ntm);
    }
    if (tm >= 1) {
      REP(k, 4) {
        const int y = i + DY4[k], x = j + DX4[k];
        if (0 <= y && y < n && 0 <= x && x < m && chmin(dp[y][x][tm - 1], dp[i][j][tm])) {
          que.emplace(dp[y][x][tm - 1], y, x, tm - 1);
        }
      }
    }
  }
  const ll ans = *min_element(next(dp[n - 1][m - 1].begin(), cons * 2), dp[n - 1][m - 1].end());
  cout << (ans == LINF ? -1 : ans) << '\n';
  return 0;
}
0