1#include <stdio.h>2#include <queue>3#include <string.h>4#include <algorithm>5 #defineINF 20000000006 using namespacestd;7typedef pair<Long Long,int>PII;8Priority_queue <pii, Vector<pii>, Greater<pii > >Q;9 structnodeTen { One intu, V, W, next; A}a[100005]; - inttot, N, M, Rxa, Rxc, RYA, RYC; - intdis[100005]; the intRP, T, first[100005]; - intdone[100005]; - voidAddedge (intStintEndintval) - { +a[++tot].u = ST;A[TOT].V = END;A[TOT].W =Val; -A[tot].next =first[st];first[st] =tot; + } A intDij () at { - for(inti =1; I <= N; i++) Dis[i] =inf; -Q.push (Make_pair (0,1)); -dis[1] =0; - while(!q.empty ()) - { in intU =q.top (). Second;q.pop (); - if(Done[u])Continue; toDone[u] =1; + for(inte = First[u]; E! =-1; E =a[e].next) - { the intv =a[e].v; * if(Dis[u] + A[E].W <Dis[v]) $ {Panax NotoginsengDIS[V] = Dis[u] +A[E].W; - Q.push (Make_pair (dis[v], v)); the } + } A } the } + intMain () - { $scanf"%d%d", &m, &n); $ intx, y; -memset (First,-1,sizeof(first)); - for(inti =1; I <= m; i++) the { -scanf"%d%d", &x, &y);WuyiAddedge (x, Y,1); the } - Dij (); Wu if(Dis[n] = = inf) printf ("-1\n"); - Elseprintf"%d\n", dis[n]+1); About return 0; $}
bzoj1674: [Usaco2005]part acquisition Nude Dijkstra