// 基本テンプレート #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++) #define repq(i,a,n) for(int (i)=(a); (i)<=(n); (i)++) #define repr(i,a,n) for(int (i)=(a); (i)>=(n); (i)--) #define int long long int template void chmax(T &a, T b) {a = max(a, b);} template void chmin(T &a, T b) {a = min(a, b);} template void chadd(T &a, T b) {a = a + b;} typedef pair pii; typedef long long ll; int dx[] = {0, 0, 1, -1}; int dy[] = {1, -1, 0, 0}; constexpr ll INF = 1001001001001001LL; constexpr ll MOD = 1000000007LL; struct Edge { int to_idx, to_height; }; struct Elem { int flr, cost; bool operator<(const Elem &x) const { return cost > x.cost; } }; bool exist[200010]; map< int, map > > edges; vector in[200010], out[200010]; map rec; vector get_array(int idx, int val) { int idx_l = --upper_bound(in[idx].begin(), in[idx].end(), val) - in[idx].begin(); int idx_r = lower_bound(in[idx].begin(), in[idx].end(), val) - in[idx].begin(); int P = in[idx].size(); vector cand; if(idx_l >= 0 && idx_l < P) cand.push_back(in[idx][idx_l]); if(idx_r >= 0 && idx_r < P) cand.push_back(in[idx][idx_r]); return cand; } signed main() { int N, M, K, S, T; cin >> N >> M >> K >> S >> T; in[1].push_back(S); rep(i,0,M) { int a, b, c; cin >> a >> b >> c; exist[a] = true; edges[a][b].push_back(Edge{a+1, c}); out[a].push_back(b); in[a+1].push_back(c); } int cnt = 0; repq(i,1,N) { sort(in[i].begin(), in[i].end()); sort(out[i].begin(), out[i].end()); in[i].erase(unique(in[i].begin(), in[i].end()), in[i].end()); out[i].erase(unique(out[i].begin(), out[i].end()), out[i].end()); cnt += exist[i]; } if(cnt != N-1) { cout << -1 << endl; return 0; } if(N == 1) { cout << abs(S - T) << endl; return 0; } rec[S] = 0; repq(i,1,N) { // floor, cost priority_queue tmp; for(auto x : rec) tmp.push(Elem{x.first, x.second}); while(!tmp.empty()) { Elem cur = tmp.top(); tmp.pop(); int idx = lower_bound(in[i].begin(), in[i].end(), cur.flr) - in[i].end(); int dd[] = {-1, 1}; rep(k,0,2) { int nidx = idx + dd[k]; if(nidx < 0 || nidx >= (int)in[i].size()) continue; int to = in[i][nidx]; int diff = abs(cur.flr - to); if(!rec.count(to) || rec[to] > rec[cur.flr] + diff) { int ncost = rec[to] = rec[cur.flr] + diff; tmp.push(Elem{to, ncost}); } } } /* printf("index = %lld\n", i); for(auto x : rec) { printf("height = %lld, cost = %lld\n", x.first, x.second); } */ map swp; if(i == N) continue; for(auto from : out[i]) { int cur_cost = INF; vector cand = get_array(i, from); // for(auto point : cand) printf("candidate: %lld\n", point); for(auto point : cand) chmin(cur_cost, rec[point] + abs(point - from)); for(auto e : edges[i][from]) { int to = e.to_height; if(!swp.count(to) || swp[to] > cur_cost) { swp[to] = cur_cost; } } } swap(rec, swp); } int ans = INF; for(auto x : rec) chmin(ans, x.second + abs(x.first - T)); cout << ans << endl; return 0; }