#include using namespace std; #define int long long using ll=long long; using vi=vector; using pii=pair; #define ALL(c) begin(c),end(c) #define RALL(c) rbegin(c),rend(c) #define ITR(i,b,e) for(auto i=(b);i!=(e);++i) #define FORE(x,c) for(auto &x:c) #define REPF(i,a,n) for(int i=a,i##len=(int)(n);i=0;--i) #define SZ(c) ((int)c.size()) #define CONTAIN(c,x) (c.find(x)!=end(c)) #define OUTOFRANGE(y,x,h,w) (y<0||x<0||y>=h||x>=w) #define dump(...) const int DX[9]={0,1,0,-1,1,1,-1,-1,0},DY[9]={-1,0,1,0,-1,1,1,-1,0}; #define INF (1001001001) #define INFLL (1001001001001001001ll) template ostream& operator << (ostream &os,const vector &v) { ITR(i,begin(v),end(v)) os<<*i<<(i==end(v)-1?"":" "); return os; } template istream& operator >> (istream &is,vector &v) { ITR(i,begin(v),end(v)) is>>*i; return is; } template istream& operator >> (istream &is, pair &p) { is>>p.first>>p.second; return is; } template bool chmax(T &a,const T &b) {if(a bool chmin(T &a,const T &b) {if(b using heap = priority_queue,greater>; struct before_main_function { before_main_function() { #ifdef int #undef INF #define INF INFLL #define stoi stoll #endif cin.tie(0);ios::sync_with_stdio(false); cout<>N>>M>>K>>S>>T; vector> bridge(M,vector(3)); cin>>bridge; vector>>> g(N); g[0][S]={}; g[N-1][T]={}; REP(i,M) { int a=bridge[i][0]-1; int b=bridge[i][1]; int c=bridge[i][2]; g[a][b].push_back({{a+1,c},0}); if(!CONTAIN(g[a+1],c)) { g[a+1][c]={}; } } REP(i,N) { ITR(itr,begin(g[i]),end(g[i])) { if(itr!=begin(g[i])) { auto prev=--itr; itr++; itr->second.push_back({{i,prev->first},abs((itr->first)-(prev->first))}); } auto next=++itr; itr--; if(next!=end(g[i])) { itr->second.push_back({{i,next->first},abs((itr->first)-(next->first))}); } } } // REP(i,N) { // cout<> hp; hp.push({0,{0,S}}); vector> dist(N); while(!hp.empty()) { int d=hp.top().first; int v=hp.top().second.first; int f=hp.top().second.second; hp.pop(); FORE(e,g[v][f]) { int vv=e.first.first; int ff=e.first.second; int cost=e.second; if(!CONTAIN(dist[vv],ff) || dist[vv][ff]>d+cost) { dist[vv][ff]=d+cost; hp.push({d+cost,{vv,ff}}); } } } if(CONTAIN(dist[N-1],T)) { cout<