#define _USE_MATH_DEFINES #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; class Edge { public: int to, cost; Edge(int to, int cost){ this->to = to; this->cost = cost; } }; const long long INF = LLONG_MAX / 2; int main() { int n, m; cin >> n >> m; vector > edges(n); for(int i=0; i> a >> b >> c; -- a; -- b; edges[a].push_back(Edge(b, c)); edges[b].push_back(Edge(a, c)); } vector > dp(2, vector(n, INF)); dp[0][0] = dp[1][0] = 0; priority_queue > pq; pq.push(make_tuple(-0, 0, 0)); pq.push(make_tuple(-0, 1, 0)); while(!pq.empty()){ long long cost; int cnt, curr; tie(cost, cnt, curr) = pq.top(); cost *= -1; pq.pop(); if(dp[cnt][curr] < cost) continue; for(const Edge& e : edges[curr]){ int next = e.to; if(cnt == 0 && cost < dp[1][next]){ dp[1][next] = cost; pq.push(make_tuple(-cost, 1, next)); } long long cost2 = cost + e.cost; if(cost2 < dp[cnt][next]){ dp[cnt][next] = cost2; pq.push(make_tuple(-cost2, cnt, next)); } } } for(int i=0; i