#include #include #include #include #include #include #include #include #include #include #include using namespace std; #define rep(i, n) for(int i = 0; i < n; i++) #define all(x) (x).begin(), (x).end() #define itr(i, x) for(auto i = (x).begin(); i != (x).end(); i++) #define ritr(i, x) for(auto i = (x).rbegin(); i != (x).rend(); i++) #define INF 1010101010 #define MOD 1000000007 #define LL long long int main() { int n, m, s, g; cin >> n >> m >> s >> g; vector>> gr(n); vector mdist(n, INF); vector used(n, false); vector pre(n); rep(i, m) { int a, b, c; cin >> a >> b >> c; gr[a].push_back(make_pair(b, c)); gr[b].push_back(make_pair(a, c)); } mdist[g] = 0; pre[g] = g; while(!used[s]) { int next = -1; rep(i, n) next = !used[i] && (next == -1 || mdist[i] < mdist[next]) ? i : next; used[next] = true; for (const auto e: gr[next]) { if (mdist[next] + e.second < mdist[e.first]) { pre[e.first] = next; mdist[e.first] = mdist[next] + e.second; } if (mdist[next] + e.second == mdist[e.first] && pre[e.first] > next) pre[e.first] = next; } } vector trace; trace.push_back(s); while(trace.back() != g) trace.push_back(pre[trace.back()]); for (const int& e: trace) cout << e << ' '; cout << endl; }