#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using ll = long long; using Pll = pair; using Pii = pair; constexpr int INF = 1 << 30; constexpr ll LINF = 1LL << 60; constexpr ll MOD = 1000000007; constexpr long double EPS = 1e-10; constexpr int dyx[4][2] = { { 0, 1}, {-1, 0}, {0,-1}, {1, 0} }; constexpr int MAX_N = 112345; vector d(MAX_N, LINF); vector path, prevs[MAX_N]; int n, m; priority_queue, greater > que; vector edges[MAX_N]; void dijkstra(int start, int goal){ d[start] = 0; que.push(Pll(0, start)); while(!que.empty()) { Pll v = que.top(); que.pop(); if(d[v.second] < v.first) continue; for(Pll e: edges[v.second]) { if(d[e.first] > d[v.second] + e.second) { d[e.first] = d[v.second] + e.second; prevs[e.first].clear(); prevs[e.first].push_back(v.second); que.push(Pll(d[e.first], e.first)); } else if(d[e.first] == d[v.second] + e.second) { prevs[e.first].push_back(v.second); } } } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int start, goal; cin >> n >> m >> start >> goal; int a[m], b[m], c[m]; for(int i=0;i> a[i] >> b[i] >> c[i]; edges[b[i]].emplace_back(a[i], 1); } dijkstra(goal, start); if(d[start] == LINF) { cout << -1 << endl; return 0; } int v = start; while(v != goal) { path.push_back(v); v = *min_element(prevs[v].begin(), prevs[v].end()); } path.push_back(goal); for(int i=0;i