3040: Shortest Way (road) time limit:60 Sec Memory limit:200 MB
submit:2476 solved:814
[Submit] [Status] [Discuss] Description
n points, the forward graph of the M-bar, to find the shortest (guaranteed existence) point 1 to the nearest n.
1<=n<=1000000,1<=m<=10000000
Input
The first line, two integers, n, M, indicates the number of points and sides.
The second line is six integers t, Rxa, RXC, RYA, Ryc, Rp.
The front T edge is generated as follows:
1. Initialize the x=y=z=0.
2. Repeat the following procedure T:
x= (X*RXA+RXC)%RP;
Y= (Y*RYA+RYC)%RP;
A=min (x%n+1,y%n+1);
B=max (y%n+1,y%n+1);
There is a 1e8-100*a from A to B with a length of.
The post-m-t strip is read in the following way:
The next line of M-t is three integers per line, x, Y, Z, representing a forward edge from X to Y length Z.
1<=x,y<=n,0<z,rxa,rxc,rya,ryc,rp<2^31
Output
An integer that represents the shortest-circuiting of the 1~n.
Sample Input3 3
0 1 2 3 5 7
1 2 1
1 3 3
2 3 1
Sample Output2
HINT
Comments
Use an efficient heap to optimize the Dijkstra algorithm.
Source
WC2013 Campers Exchange-lydrainbowcat
Paired heap not only fast, but also supports modification operations
Point_iterator is its iterator.
Q.modify (iterator, modified Element)
Do not know why the handwriting structure is not possible, with a pair can be
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<ext/pb_ds/priority_queue.hpp>typedefLong Longll;#definePA pair<ll,int>#defineMP Make_pairusing namespacestd;using namespace__gnu_pbds;typedef __gnu_pbds::p riority_queue<pa,greater<pa> >Heap;Const intn=1e6+5, m=1e7+5;Constll inf=1e15;inlineintRead () {CharC=getchar ();intx=0, f=1; while(c<'0'|| C>'9'){if(c=='-') f=-1; c=GetChar ();} while(c>='0'&&c<='9') {x=x*Ten+c-'0'; c=GetChar ();} returnx*F;}intn,m,t,rxa,rxc,rya,ryc,rp,a,b;intx, y, zstructedge{intV,w,ne;} E[M];intCnt,h[n];inlinevoidInsintUintVintW) {CNT++; E[CNT].V=v;e[cnt].w=w;e[cnt].ne=h[u];h[u]=CNT;} ll d[n];heap q;heap::p oint_iterator id[n];voidDij () { for(intI=1; i<=n;i++) d[i]=INF; d[1]=0; id[1]=q.push (MP (0,1)); while(!Q.empty ()) { intU=q.top (). Second;q.pop ();//printf ("U%d\n", u); for(intI=h[u];i;i=e[i].ne) { intv=e[i].v,w=E[I].W; if(d[v]>d[u]+W) {D[v]=d[u]+W; if(id[v]!=0) q.modify (ID[V],MP (d[v],v)); Elseid[v]=Q.push (MP (D[V],V)); } } }}intMain () {//freopen ("In.txt", "R", stdin);N=read (); m=read (); T=read (); Rxa=read (); Rxc=read (); Rya=read (); Ryc=read (); rp=read (); M=m-T; while(t--) {x=y=z=0; X= (ll) x*rxa+rxc)%RP; Y= (ll) y*rya+ryc)%RP; A=min (x%n+1, y%n+1); b=max (y%n+1, y%n+1); Ins (A, B,100000000- -*a); } while(m--) X=read (), Y=read (), z=read (), ins (x, y, z); Dij (); printf ("%lld", D[n]);}
So I have to hand over the Luogu template problem, not open O2 380ms, than SLF optimized SPFA is also faster
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<ext/pb_ds/priority_queue.hpp>#definePA pair<int,int>#defineMP Make_pairusing namespacestd;using namespace__gnu_pbds;typedef __gnu_pbds::p riority_queue<pa,greater<pa> >Heap;Const intn=1e4+5, m=5e5+5, inf=2147483647; inlineintRead () {CharC=getchar ();intx=0, f=1; while(c<'0'|| C>'9'){if(c=='-') f=-1; c=GetChar ();} while(c>='0'&&c<='9') {x=x*Ten+c-'0'; c=GetChar ();} returnx*F;}intn,m,s,u,v,w;structedge{intV,ne,w;} E[M];intH[n],cnt=0; inlinevoidInsintUintVintW) {CNT++; E[CNT].V=v;e[cnt].w=w;e[cnt].ne=h[u];h[u]=CNT;} Heap q;heap::p oint_iterator it[n];intD[n];voidDij (ints) { for(intI=1; i<=n;i++) d[i]=INF; D[s]=0; It[s]=q.push (MP (0, s)); while(!Q.empty ()) { intu=q.top (). Second;q.pop (); for(intI=h[u];i;i=e[i].ne) { intv=e[i].v,w=E[I].W; if(d[v]>d[u]+W) {D[v]=d[u]+W; if(it[v]!=0) q.modify (IT[V],MP (d[v],v)); Elseit[v]=Q.push (MP (D[V],V)); } } }}intMain () {n=read (); M=read (); s=read (); for(intI=1; i<=m;i++) {u=read (); V=read (); w=read (); Ins (u,v,w);} Dij (s); for(intI=1; i<=n;i++) printf ("%d", D[i]);}
Bzoj 3040: Shortest Way (road) [Dijkstra + Pb_ds]