Xjoi Online synchronization Training DAY6 T1

Source: Internet
Author: User
Tags gcd

Train of thought: the time of the examination directly thought out, and a little worried about complexity, but still hit, incredibly is direct a off, happy ah.

We found that, ai<=7, this must be a very important condition, we consider state compression, to enumerate which numbers appear in the path, and then we divide the original n points into the least common multiple of our enumeration number, because if a number modulo a number equals 0, then modulo its factor must be 0, So our idea is to split the shortest way.

1#include <cstdio>2#include <cmath>3#include <cstring>4#include <algorithm>5#include <iostream>6 Const intp[ -]={1,2,3,4,5,6,7,Ten, A, -, the, -, +, -, -, *, the, -, -, -, the, $, About,420};7 inttot,go[200005],first[200005],next[200005];8 structedge{9     intu,v;Ten}e[50005]; One inta[200005],n,m,vis[5005][505],dis[5005][505],c[2000005][2]; A intRead () { -      intt=0, f=1;CharCh=GetChar (); -       while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} the       while('0'<=ch&&ch<='9') {t=t*Ten+ch-'0'; ch=GetChar ();} -      returnt*F; - } - voidInsertintXinty) { +tot++; -go[tot]=y; +next[tot]=First[x]; Afirst[x]=tot; at } - voidAddintXinty) { - Insert (x, y); insert (y,x); - } - intgcdintAintb) { -      if(b==0)returnA; in          Else returnGCD (b,a%b); - } to voidBuildintMod) { +      for(intI=1; i<=n;i++) first[i]=0; tot=0; -      for(intI=1; i<=m;i++){ the         if(mod%a[e[i].u]!=0)Continue; *         if(mod%a[e[i].v]!=0)Continue; $ Add (E[I].U,E[I].V);Panax Notoginseng     } - } the intSPFA (intMod) { + build (Mod); A       for(intI=1; i<=n;i++) the        for(intj=0; j<mod;j++) +dis[i][j]=0x3f3f3f3f, vis[i][j]=0; -      intH=1, t=1; c[h][0]=1; c[h][1]=a[1]%Mod; $vis[1][0]=1;d is[1][a[1]%mod]=a[1];  $       while(h<=t) { -             intnowx=c[h][0],nowy=c[h++][1]; -              for(intI=first[nowx];i;i=Next[i]) { the                 intPur1=go[i],pur2= ((nowy*Ten%MOD) +a[pur1])%Mod; -                 if(dis[pur1][pur2]>dis[nowx][nowy]+A[pur1]) {Wuyidis[pur1][pur2]=dis[nowx][nowy]+A[pur1]; the                     if(VIS[PUR1][PUR2])Continue; -vis[pur1][pur2]=1; Wut++; -c[t][0]=pur1;c[t][1]=Pur2; About                 } $             } -vis[nowx][nowy]=0; -      } -     returndis[n][0]; A } + voidsolve () { the      intans=0x3f3f3f3f; -      intsb=a[1]*A[N]/GCD (a[1],a[n]); $       for(intI=0;i< -; i++) the       if(p[i]%sb==0){ theans=std::min (ANS,SPFA (p[i)); the       } the      if(ans==0x3f3f3f3f) ans=-1; -printf"%d\n", ans); in } the intMain () { the      intt=read (); About       while(t--){ theN=read (); m=read (); the          for(intI=1; i<=n;i++) first[i]=0; tot=0; the          for(intI=1; i<=n;i++) a[i]=read (); +          for(intI=1; i<=m;i++){ -E[i].u=read (); e[i].v=read (); the         }Bayi solve (); the      } the}

Xjoi Online synchronization Training DAY6 T1

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.