#include using namespace std; using ll = long long; const int mask = 1000; ll memo[81][81][3000]; int vis[81][81][3000]; int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); ll ans = 1e18; ll n,m,k,t; cin>>n>>m>>k>>t; vector>> use(n,vector>(m)); for(int i = 0;i>a>>b>>c>>d; a--;b--; use[a][b] = make_pair(c,d); } using dat = pair>; priority_queue,vector>,greater>> que; que.push(make_pair(0,make_pair(0,make_pair(0,0)))); int dx[] = {1,-1,0,0}; int dy[] = {0,0,1,-1}; for(int i = 0;it) continue; ans = min(ans,now.first); continue; } for(int k = 0;k<4;k++){ int nni = ni + dx[k]; int nnj = nj + dy[k]; if(nni<0||nnj<0||nni>=n||nnj>=m) continue; ll nxt = now.second.first; nxt++; ll cost = now.first; if(nxt<=-mask) nxt = -mask; if(nxt>=mask) nxt = mask; dat will = make_pair(nxt,make_pair(nni,nnj)); if(memo[nni][nnj][nxt+mask]<=cost) continue; memo[nni][nnj][nxt+mask] = cost; que.push(make_pair(cost,will)); } if(use[ni][nj].first!=0){ ll nxt = now.second.first + 1; ll cost = now.first; nxt -= use[ni][nj].first; cost += use[ni][nj].second; int nni = ni; int nnj = nj; dat will = make_pair(nxt,make_pair(nni,nnj)); if(nxt<=-mask) nxt = -mask; if(nxt>=mask) nxt = mask; if(memo[ni][nj][nxt+mask]<=cost) continue; memo[ni][nj][nxt+mask] = cost; que.push(make_pair(cost,will)); } } if(ans==1e18) ans = -1; cout<