#include #include #include #include using namespace std; using i64 = int64_t; struct edge { int to; i64 cost; }; constexpr i64 inf = 987'654'321'987'654'321LL; template inline void readint(T *x) { *x = 0; int c; for(;;) { c = getchar_unlocked(); switch(c) case 48: case 49: case 50: case 51: case 52: case 53: case 54: case 55: case 56: case 57: goto num; } num: for(;;) { switch(c) { case 48: case 49: case 50: case 51: case 52: case 53: case 54: case 55: case 56: case 57: *x *= 10; *x += c - 48; break; default: return; } c = getchar_unlocked(); } } int main(void) { int N, M; readint(&N); readint(&M); vector> G(N, vector()); for(int i=0; i> cost(2, vector(N, inf)); cost[0][0] = 0; cost[1][0] = 0; vector> seen(2, vector(N, false)); // コスト、頂点、チケットを使ったか priority_queue, vector>, greater>> pq; pq.emplace(0, 0, false); while(!pq.empty()) { i64 _; int v; bool used; tie(_, v, used) = pq.top(); pq.pop(); if(seen[used][v]) { continue; } seen[used][v] = true; for(edge &e : G[v]) { // チケットを使わない if(cost[used][e.to] > cost[used][v] + e.cost) { cost[used][e.to] = cost[used][v] + e.cost; pq.emplace(cost[used][e.to], e.to, used); } // チケットを使う if(!used && cost[true][e.to] > cost[false][v]) { cost[true][e.to] = cost[false][v]; pq.emplace(cost[true][e.to], e.to, true); } } } for(int v=0; v