結果

問題 No.2366 登校
ユーザー SSRS
提出日時 2023-06-30 21:46:35
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,291 ms / 4,000 ms
コード長 1,823 bytes
コンパイル時間 1,979 ms
コンパイル使用メモリ 180,888 KB
実行使用メモリ 20,992 KB
最終ジャッジ日時 2025-06-20 11:10:37
合計ジャッジ時間 9,149 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

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