#include #include #include #include #include #include #include #include #include #include using namespace std; typedef struct{ int to; int cost; }edge; //distance from s //O(E log V) void dijkstra(vector< vector > &G, vector &dist, vector &last, int s){ //INF as distance const int INF = 1<<29; //first : distance from s, second : its vertex priority_queue< pair , vector< pair >, greater< pair >> pq; fill(dist.begin(), dist.end(), INF); dist[s] = 0; last[s] = s; pq.push( {dist[s], s} ); while(!pq.empty()){ auto q = pq.top(); pq.pop(); int from = q.second; if(dist[from] < q.first) continue; // it's not minimum distance int n=G[from].size(); for(int i=0; i next){ dist[e.to] = next; last[e.to] = from; pq.push( {dist[e.to], e.to} ); } } } } void add_edge(vector > &G, int from, int to, int cost){ G[from].push_back( (edge){to, cost} ); G[to].push_back( (edge){from, cost} ); } int main(){ int N,M,S,G; cin >> N >> M >> S >> G; vector> Graph(N); for(int i=0; i> a >> b >> c; add_edge(Graph, a, b, c); } vector dist(N); vector last(N, -1); dijkstra(Graph, dist, last, G); vector ans; { for(int pos = S; pos != last[pos]; pos = last[pos]){ ans.push_back(pos); } ans.push_back(G); } for(int i=0; i