結果

問題 No.2366 登校
ユーザー SSRSSSRS
提出日時 2023-06-30 21:46:35
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,379 ms / 4,000 ms
コード長 1,823 bytes
コンパイル時間 1,970 ms
コンパイル使用メモリ 188,204 KB
実行使用メモリ 20,864 KB
最終ジャッジ日時 2023-09-03 02:59:17
合計ジャッジ時間 9,695 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 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 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 370 ms
8,008 KB
testcase_13 AC 346 ms
7,696 KB
testcase_14 AC 354 ms
7,792 KB
testcase_15 AC 368 ms
7,688 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 356 ms
8,068 KB
testcase_18 AC 367 ms
7,900 KB
testcase_19 AC 377 ms
7,944 KB
testcase_20 AC 361 ms
7,960 KB
testcase_21 AC 340 ms
7,796 KB
testcase_22 AC 312 ms
7,916 KB
testcase_23 AC 7 ms
4,376 KB
testcase_24 AC 225 ms
20,832 KB
testcase_25 AC 1,379 ms
20,792 KB
testcase_26 AC 1,358 ms
20,864 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const long long INF = 1000000000000000;
vector<int> dx = {1, 0, -1, 0};
vector<int> dy = {0, 1, 0, -1};
int main(){
  int N, M, K, T;
  cin >> N >> M >> K >> T;
  vector<int> A(K), B(K), C(K), D(K);
  for (int i = 0; i < K; i++){
    cin >> A[i] >> B[i] >> C[i] >> D[i];
    A[i]--;
    B[i]--;
  }
  if (T >= N + M - 2){
    cout << 0 << endl;
  } else {
    vector<vector<int>> id(N, vector<int>(M, -1));
    for (int i = 0; i < K; i++){
      id[A[i]][B[i]] = i;
    }
    int T2 = (N + M) * 2 + 1;
    vector<vector<vector<long long>>> d(T2, vector<vector<long long>>(N, vector<long long>(M, INF)));
    priority_queue<tuple<long long, int, int, int>, vector<tuple<long long, int, int, int>>, greater<tuple<long long, int, int, int>>> pq;
    pq.push(make_tuple(0, N + M, 0, 0));
    while (!pq.empty()){
      long long c = get<0>(pq.top());
      int t = get<1>(pq.top());
      int x = get<2>(pq.top());
      int y = get<3>(pq.top());
      pq.pop();
      if (d[t][x][y] == INF){
        d[t][x][y] = c;
        if (id[x][y] != -1){
          int t2 = max(t - C[id[x][y]] + 1, 0);
          if (d[t2][x][y] == INF){
            pq.push(make_tuple(c + D[id[x][y]], t2, x, y));
          }
        }
        if (t < T2 - 1){
          for (int i = 0; i < 4; i++){
            int x2 = x + dx[i];
            int y2 = y + dy[i];
            if (0 <= x2 && x2 < N && 0 <= y2 && y2 < M){
              if (d[t + 1][x2][y2] == INF){
                pq.push(make_tuple(c, t + 1, x2, y2));
              }
            }
          }
        }
      }
    }
    long long ans = INF;
    for (int i = -(N + M); i <= T; i++){
      ans = min(ans, d[N + M + i][N - 1][M - 1]);
    }
    if (ans == INF){
      cout << -1 << endl;
    } else {
      cout << ans << endl;
    }
  }
}
0