#ifndef ONLINE_JUDGE #define _GLIBCXX_DEBUG #endif #include using namespace std; #define pass (void)0 #define INF (1<<30)-1 #define INFLL (1LL<<60)-1 #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define repr(i, n) for (int i = (int)(n) - 1; i >= 0; i--) #define rep2(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define repr2(i, a, b) for (int i = (int)(b) - 1; i >= (int)(a); i--) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define sz(x) ((int)(x).size()) #define YesNo(cond) cout << ((cond) ? "Yes\n" : "No\n") #define YESNO(cond) cout << ((cond) ? "YES\n" : "NO\n") using ll = long long; using pii = pair; using pll = pair; using vi = vector; using vl = vector; using vvi = vector; using vvl = vector; template void print(const T& value) { cout << value << "\n"; } template void print(const vector& vec) { for (auto& v : vec) cout << v << " "; cout << "\n"; } template void input(vector& vec) { for (auto& v : vec) cin >> v; }; template bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; } return false; } template bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } return false; } #include using namespace atcoder; using mint = modint998244353; using vm = vector; vi dh = {-1, 0, 1, 0}; vi dw = {0, 1, 0, -1}; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(10); ll N, M, K, T; cin >> N >> M >> K >> T; vvl C(N, vl(M, -1)); vvl D(N, vl(M, -1)); rep (_, K) { ll a, b, c, d; cin >> a >> b >> c >> d; C[a-1][b-1] = c; D[a-1][b-1] = d; } ll limit = (N+M)*5; vector dist(N, vvl(M, vl(limit*2+1, INFLL))); vector>> visited(N, vector>(M, vector(limit*2+1, false))); dist[0][0][limit] = 0; priority_queue>, vector>>, greater>>> que; que.push({0, {0, 0, limit}}); while (!que.empty()) { auto [_, t] = que.top(); auto [h, w, n] = t; que.pop(); if (visited[h][w][n]) continue; visited[h][w][n] = true; if (C[h][w] != -1) { ll nn = max(n-C[h][w]+1, 0LL); ll ndist = dist[h][w][n]+D[h][w]; if (ndist < dist[h][w][nn]) { dist[h][w][nn] = ndist; que.push({ndist, {h, w, nn}}); } } rep (i, 4) { ll nh, nw, nn; nh = h+dh[i]; nw = w+dw[i]; nn = n+1; ll ndist = dist[h][w][n]; if (0<=nh && nh