#include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; #define int long long #define double long double typedef vector VI; typedef pair pii; typedef vector VP; typedef vector VS; typedef priority_queue PQ; templatebool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } templatebool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } #define fore(i,a) for(auto &i:a) #define REP(i,n) for(int i=0;i, greater > q2; struct edge { int to, cost; }; #define LLINF 922337203685477580 int N, gx, gy; vector > G; VI d; void dikstra(int s) { priority_queue, greater >que; REP(i, d.size())d[i] = LLINF; d[s] = 0; que.push(pii(0, s)); while (!que.empty()) { pii p = que.top(); que.pop(); int v = p.second; if (d[v] < p.first)continue; REP(i, G[v].size()) { edge e = G[v][i]; if (d[e.to] > d[v] + e.cost) { d[e.to] = d[v] + e.cost; que.push(pii(d[e.to], e.to)); } } } } bool is_in(int x, int y) { return (x > gx || y > gy);//枠の外 } signed main() { cin.tie(0); ios::sync_with_stdio(false); int F; cin >> gx >> gy >> N >> F; vector > A(N); REP(i, N)cin >> A[i].first.first >> A[i].first.second >> A[i].second; map mp; int tmp = 0; eREP(i, gx)eREP(j, gy)mp[pii(i, j)] = tmp++; G.resize(tmp); d.resize(tmp); //cout << tmp << endl; eREP(i, gx) { eREP(j, gy) { int from = mp[pii(i, j)]; REP(k, N) { int x = A[k].first.first + i; int y = A[k].first.second + j; int c = A[k].second; if (is_in(x, y))continue; int to = mp[pii(x, y)]; //cout << k<<" "<< from << " " << to << " " << c << endl; //cout << k<<" "<< from << " " << to << " " << c << endl; G[from].push_back((edge) { to, c }); } if (!is_in(i + 1, j)) { int to = mp[pii(i + 1, j)]; G[from].push_back((edge) { to, F }); } if (!is_in(i, j + 1)) { int to = mp[pii(i, j + 1)]; G[from].push_back((edge) { to, F }); } } } dikstra(0); eREP(i, gx)eREP(j, gy) { //cout << gx << " " << gy << " " << d[mp[pii(gx, gy)]] << endl; } cout << d[mp[pii(gx, gy)]] << endl; return 0; }