#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } const long long MAX = 5100000; const long long INF = 1LL << 60; const long long mod = 1000000007LL; //const long long mod = 998244353LL; using namespace std; typedef unsigned long long ull; typedef long long ll; void dijkstra(ll start, vector>>& graph, vector& dist) { dist[start] = 0; priority_queue, vector>, greater>> pq; vector used(dist.size(), false); pq.push(make_pair(0, start)); while (!pq.empty()) { ll d, node; tie(d, node) = pq.top(); pq.pop(); if (used[node]) continue; used[node] = true; for (pair element : graph[node]) { ll new_d, new_node; tie(new_node, new_d) = element; new_d += d; if (new_d < dist[new_node]) { dist[new_node] = new_d; pq.push(make_pair(dist[new_node], new_node)); } } } } int main() { /* cin.tie(nullptr); ios::sync_with_stdio(false); */ ll gh, gw, n, f; scanf("%lld %lld %lld %lld", &gh, &gw, &n, &f); vector x(n), y(n), c(n); for (ll i = 0; i < n; i++) scanf("%lld %lld %lld", &x[i], &y[i], &c[i]); x.push_back(1); x.push_back(0); y.push_back(0); y.push_back(1); c.push_back(f); c.push_back(f); n += 2; ll H = gh + 1; ll W = gw + 1; vector>> g(H * W); for (ll i = 0; i < H; i++) { for (ll j = 0; j < W; j++) { for (ll k = 0; k < n; k++) { ll nh = i + x[k]; ll nw = j + y[k]; if (nh >= H || nw >= W) continue; g[i * W + j].emplace_back(nh * W + nw, c[k]); } } } vector d(H * W, INF); dijkstra(0, g, d); cout << d[gh * W + gw] << endl; return 0; }