#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'321; template 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(N, vector(2, inf)); cost[0][0] = 0; cost[0][1] = 0; vector> seen(N, vector(2, false)); // コスト、頂点、チケットを使ったか priority_queue, vector>, greater>> pq; pq.emplace(0, 0, false); while(!pq.empty()) { auto [_, v, used] = pq.top(); pq.pop(); if(seen[v][used]) { continue; } seen[v][used] = true; for(edge &e : G[v]) { // チケットを使わない if(cost[e.to][used] > cost[v][used] + e.cost) { cost[e.to][used] = cost[v][used] + e.cost; pq.emplace(cost[e.to][used], e.to, used); } // チケットを使う if(!used && cost[e.to][true] > cost[v][false]) { cost[e.to][true] = cost[v][false]; pq.emplace(cost[e.to][true], e.to, true); } } } for(int v=0; v