#include #include #include #include #include #include #include #include #include #include using namespace std; typedef struct{ int to; int cost; }edge; class my_distance{ public: long long cost; vector s; my_distance(){ } my_distance(long long cost_, const vector s_){ cost = cost_; s = s_; } my_distance(const my_distance& x){ this->cost = x.cost; this->s = x.s; } bool operator<(const my_distance& x) const{ if(this->cost != x.cost) return this->cost < x.cost; return this->s < x.s; } bool operator>(const my_distance& x) const{ if(this->cost != x.cost) return this->cost > x.cost; return this->s > x.s; } }; //distance from s //O(E log V) void dijkstra(vector< vector > &G, vector< my_distance > &dist, int s){ //INF as distance const long long INF = 1LL<<60LL; //first : distance from s, second : its vertex priority_queue< pair , vector< pair >, greater< pair >> pq; fill(dist.begin(), dist.end(), my_distance(INF, {}) ); dist[s] = my_distance(0, {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; 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); dijkstra(Graph, dist, S); auto ans = dist[G].s; for(int i=0; i