結果

問題 No.2366 登校
ユーザー pointN
提出日時 2023-06-30 23:01:19
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,183 ms / 4,000 ms
コード長 2,141 bytes
コンパイル時間 1,595 ms
コンパイル使用メモリ 154,812 KB
実行使用メモリ 24,900 KB
最終ジャッジ日時 2025-06-20 11:11:54
合計ジャッジ時間 10,510 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <cmath>
#include <iomanip>
#include <stack>
#include <queue>
#include <numeric>
#include <map>
#include <unordered_map>
#include <set>
#include <fstream>
#include <chrono>
#include <random>
#include <bitset>
//#include <atcoder/all>
#define rep(i,n) for(int i=0;i<(n);i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) ((int)(x).size())
#define pb push_back
using ll = long long;
using namespace std;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
ll gcd(ll a, ll b) {return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) {return a/gcd(a,b)*b;}

vector<int> dy = {-1,0,1,0};
vector<int> dx = {0,-1,0,1};

int main(){
  int N,M,K,T; cin >> N >> M >> K >> T;
  vector<vector<pair<int,int>>> A(N,vector<pair<int,int>>(M,{0,0}));
  rep(i,K){
    int a,b,c,d; cin >> a >> b >> c >> d; a--; b--;
    if(c!=1) A[a][b] = {c,d};
  }
  if(N+M-2<=T){
    cout << "0\n";
    return 0;
  }
  vector D(N,vector(M,vector<ll>(400,1LL<<60)));
  vector used(N,vector(M,vector<bool>(400,false)));

  ll ans = 1LL<<60;
  priority_queue<pair<ll,vector<int>>,vector<pair<ll,vector<int>>>,greater<pair<ll,vector<int>>>> q;
  q.push({0LL,{0,0,T+200}});
  while(!q.empty()){
    ll d = q.top().first;
    vector<int> tv = q.top().second; q.pop();
    int y = tv[0], x = tv[1], l = tv[2];
    if(used[y][x][l]) continue;
    used[y][x][l] = true; D[y][x][l] = d;
    if(l!=0){
      rep(i,4){
        int ny = y + dy[i];
        int nx = x + dx[i];
        if(!(0<=ny&&ny<N&&0<=nx&&nx<M)) continue;
        if(D[ny][nx][l-1] <= d) continue;
        q.push({d,{ny,nx,l-1}});
      }
    }
    if(A[y][x]!=pair<int,int>({0,0})){
      if(D[y][x][min(399,l-1+A[y][x].first)] <= d+A[y][x].second) continue;
      q.push({d+A[y][x].second,{y,x,min(399,l-1+A[y][x].first)}});
    }
  }
  for(int i=200;i<400;i++) chmin(ans,D[N-1][M-1][i]);
  if(ans>=(1LL<<60)) ans = -1;
  cout << ans << '\n';
  return 0;
};
0