Topic links
1#include <bits/stdc++.h>2 using namespacestd;3typedefLong Longll;4 inline ll read () {5 intx=0, f=1;CharCh=GetChar ();6 while(ch>'9'|| ch<'0'){if(ch=='-') f=-1; ch=GetChar ();}7 while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();}8 returnx*F;9 }Ten One /***********************************************************/ A - Const intMAXN =100010; - intN, M; the intDIS[MAXN], HEAD[MAXN], LOOK[MAXN]; -queue<int>Q; - intans, cas; - + structEdge - { + intu, V, W, next, Vis; A}edge[maxn*4]; at - voidAdd_edge (intUintVintW) { -edge[cas].u = u; EDGE[CAS].V = v; EDGE[CAS].W =W; -Edge[cas].next = Head[u]; Edge[cas].vis =0; -Head[u] = cas++; - } in - voidDfsintRtintClintnum) { to if(RT = =N) { +Ans =num; - return; the } * if(!Look[rt]) { $LOOK[RT] =1;Panax NotoginsengDIS[RT] =num; - Q.push (RT); the } + for(inti = head[rt];i! =-1; i =Edge[i].next) { A if(Edge[i].vis)Continue; the if(EDGE[I].W = =cl) { +Edge[i].vis =1; - dfs (EDGE[I].V, cl, num); $ } $ } - return; - } the - intBFs () {Wuyi while(!q.empty ()) Q.pop (); theQ.push (1);//from the beginning. -dis[1] =0; Wulook[1] =1; - while(!Q.empty ()) { About intnow =Q.front (); Q.pop (); $ for(inti = head[now];i! =-1; i =Edge[i].next) { - if(Edge[i].vis)Continue;//walk only once per side - intv =edge[i].v; -Edge[i].vis =1; ADFS (V, EDGE[I].W, dis[now]+1);//Add all paths that have the same weight as the current path + if(Ans >0) Break; the } - if(Ans >0) Break; $ } the returnans; the } the the voidinit () { -CAS =0; inMemset (Head,-1,sizeof(head)); thememset (Look,0,sizeof(look)); thememset (DIS,0x7f,sizeof(DIS)); About } the the intMain () { the while(~SCANF ("%d%d", &n, &m)) { + init (); - for(inti =1; I <= m;i++){ the intu, V, W;BayiU = Read (); v = read (); W =read (); the Add_edge (U, V, W); the Add_edge (V, U, W); - } -Ans =-1; theAns =BFS (); theprintf"%d\n", ans); the } the return 0; -}
Age of Moyu (2018 Multi-university Training Contest 7)