The first thing you can see is that this is a short-circuit problem, but the violence is built up to n2 the edges, so consider building optimization
For P>SQRT (n) frogs can be directly violent built, up to N*SQRT (n) Edge
For P<SQRT (n) Frogs, add auxiliary points, enumerate the sqrt (n) lengths, each length establishes n auxiliary points for each edge, and the points to which they can reach and the point at which they can be reached are connected.
1 #defineMAXN 4000010UL2#include <cstdio>3#include <queue>4#include <cmath>5#include <cstring>6 7 using namespacestd;8 9 intN, M, T, INF, S, T, D[MAXN], DIS[MAXN], mp[30010][175];Ten BOOLINS[MAXN]; One A structEdge { - intHou, NT, zhi; -}sg[maxn<<2]; the - voidADD (intXintYintz) { -Sg[t] = (Edge) {y, d[x], z}, d[x] = t + +; - return; + } - + voidSPFA (intx) { Amemset (DIS,0x3f,sizeof(DIS)); atINF = dis[0]; -DIS[X] =0; -Queue <int>q; q.push (x); - while(!Q.empty ()) { - intOP =Q.front (); Q.pop (); -INS[OP] =false; in for(inti = D[op]; I! =-1; i = Sg[i].nt)if(dis[sg[i].hou]>dis[op]+Sg[i].zhi) { -Dis[sg[i].hou] = dis[op]+Sg[i].zhi; to if(!Ins[sg[i].hou]) { +Ins[sg[i].hou] =true; - Q.push (Sg[i].hou); the } * } $ }Panax Notoginseng return; - } the + intMain () { Amemset (D,-1,sizeof(d)); thescanf"%d%d", &n, &m); + intBlo = (int) sqrt (n), tot = n1; - for(inti =0; I < n; ++i) { $ for(intj =1; J <= Blo; + + j) Mp[i][j] = + + tot, Add (tot, I,0); $ } - - for(inti =0; I < n; ++i) { the for(intj =1; J <= Blo; ++j) { - if(i+j<n) ADD (Mp[i][j], mp[i+j][j],1);Wuyi if(i-j>=0) Add (Mp[i][j], mp[i-j][j],1); the } - } Wu for(inti =0, B, p; I < m; ++i) { -scanf"%d%d", &b, &p); About if(i==0) S =b; $ if(i==1) T =b; - if(P<=blo) Add (b, Mp[b][p],0); - Else { - for(intj =1; B+j*p < n; + + j) Add (b, b+j*P, j); A for(intj =1; B-j*p >=0; + + j) Add (b, b-j*P, j); + } the } - SPFA (S); $ if(Dis[t]==inf) printf ("-1"); the Elseprintf"%d", Dis[t]); the return 0; the}
View Code
APIO2015 Jakarta's Skyscraper