Age of Moyu (2018 Multi-university Training Contest 7)

Source: Internet
Author: User
Tags cas

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.