/* -*- coding: utf-8 -*- * * 614.cc: No.614 壊れたキャンパス - yukicoder */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; /* constant */ const int MAX_N = 200000; const int MAX_M = 200000; typedef long long ll; const ll LINF = 1LL << 60; /* typedef */ typedef pair pii; typedef vector vpii; /* global variables */ vpii nbrs[MAX_N]; ll dp[2][MAX_M]; /* subroutines */ inline ll absll(ll a) { return (a >= 0) ? a : -a; } inline void setmin(ll &a, ll b) { if (a > b) a = b; } /* main */ int main() { int n, m, k, st, gl; scanf("%d%d%d%d%d", &n, &m, &k, &st, &gl); for (int i = 0; i < m; i++) { int ai, bi, ci; scanf("%d%d%d", &ai, &bi, &ci); nbrs[ai].push_back(pii(bi, ci)); } nbrs[0].push_back(pii(0, st)); dp[0][0] = 0; int cur = 0, nxt = 1; for (int i = 1; i < n; i++) { vpii &nbr0 = nbrs[i - 1], &nbr1 = nbrs[i]; if (nbr1.empty()) { puts("-1"); return 0; } int n0 = nbr0.size(), n1 = nbr1.size(); fill(dp[nxt], dp[nxt] + n1, LINF); for (int j0 = 0; j0 < n0; j0++) { int &f0 = nbr0[j0].second; for (int j1 = 0; j1 < n1; j1++) setmin(dp[nxt][j1], dp[cur][j0] + absll(nbr1[j1].first - f0)); } cur ^= 1, nxt ^= 1; } ll mind = LINF; for (int j = 0; j < nbrs[n - 1].size(); j++) setmin(mind, dp[cur][j] + absll(gl - nbrs[n - 1][j].second)); printf("%lld\n", mind); return 0; }