2763: [JLOI2011] Flight route time Limit:10 Sec Memory limit:128 MB
submit:1728 solved:649
[Submit] [Status] [Discuss] Descriptionalice and Bob are now travelling by air, and they have opted for a relatively cheap airline. The airline operates in a total of N cities, which are marked as 0 to n-1, with a total of M routes, two cities per route, and a certain price on the route. Alice and Bob are now moving from one city to another, on the way to a different city. Airlines also offer discounts on their trip, and they can fly on up to K routes for free. So how much does Alice and Bob spend at least for this trip? The first line of the input data has three integers, n,m,k, indicating the number of cities, routes, and free rides. The second line has two integers, s,t, which represent the starting city number and the destination city number for each of their trips. (0<=s,t<n) Next there are m lines, three integers per line, a,b,c, indicating the existence of a route, which can be reached from city A to City B, or from City B to city A, with a price of C. (0<=a,b<n,a and B are unequal, 0<=c<=1000) Output has only one row and contains an integer, which is the minimum cost. Sample INPUT5 6 1
0 4
0 1 5
1 2 5
2 3 5
3 4 5
2 3 3
0 2 100
Sample Output8hint
For 30% of data,2<=n<=50,1<=m<=300,k=0;
For 50% of data,2<=n<=600,1<=m<=6000,0<=k<=1;
For 100% of data, 2<=n<=10000,1<=m<=50000,0<=k<=10.
Source: dijkstra+ heap Optimization was originally written with SPFA. Originally want to practice a dijkstra+ heap optimization, with a first build diagram and then directly run the shortest way, is the adjacent two layers with bidirectional edge to connect, and then each layer to connect a good diagram. I didn't expect to write that I was 2333 ... Here Orz and Rivers Yi Stream song (like and popoqqq,wyfcyx,jkxing big ye a room!!!) 7 minutes seconds to kill a problem similar to this question ...
1#include <bits/stdc++.h>2 using namespacestd;3 #defineMAXN 100104 #defineMAXM 500105 #defineINF 1e96 structnode7 {8 intBegin,end,value,next;9}edge[2*maxm* One*2];Ten intcnt,head[ One*maxn],n,dis[ One*maxn],heap[ One*maxn],pos[ One*maxn],u[maxm],v[maxm],val[maxm],size; One voidAddedge (intBbintEeintVV) A { -EDGE[++CNT].BEGIN=BB;EDGE[CNT].END=EE;EDGE[CNT].VALUE=VV;EDGE[CNT].NEXT=HEAD[BB]; head[bb]=CNT; - } the voidAddedge1 (intBbintEeintVV) - { - Addedge (BB,EE,VV); Addedge (EE,BB,VV); - } + intRead () - { + ints=0, fh=1;CharCh=GetChar (); A while(ch<'0'|| Ch>'9'){if(ch=='-') fh=-1; ch=GetChar ();} at while(ch>='0'&&ch<='9') {s=s*Ten+ (ch-'0'); ch=GetChar ();} - returns*fh; - } - voidPUSH1 (intk) - { - intnow=K,root; in while(now>1) - { toroot=now/2; + if(Dis[heap[root]]<=dis[heap[now]])return; - swap (Heap[root],heap[now]); the swap (Pos[heap[root]],pos[heap[now]]); *now=Root; $ }Panax Notoginseng } - voidInsert (intk) the { +heap[++size]=k;pos[k]=SIZE; PUSH1 (SIZE); A } the voidPOP1 (intk) + { - intnow,root=K; $pos[heap[k]]=0; heap[k]=heap[size--];if(size>0) pos[heap[k]]=K; $ while(root<=size/2) - { -now=root*2; the if(now<size&&dis[heap[now+1]]<dis[heap[now]]) now++; - if(Dis[heap[root]]<=dis[heap[now]])return;Wuyi swap (Heap[root],heap[now]); the swap (Pos[heap[root]],pos[heap[now]]); -root=Now ; Wu } - } About voidDijkstraintstart) $ { - intI,v,u; - for(i=1; i<=n;i++) dis[i]=inf;dis[start]=0; - for(i=1; i<=n;i++) Insert (i); A while(size>0) + { theu=heap[1]; POP1 (Pos[u]); - for(i=head[u];i!=-1; i=edge[i].next) $ { thev=Edge[i].end; the if(Dis[v]>dis[u]+edge[i].value) {dis[v]=dis[u]+Edge[i].value; PUSH1 (Pos[v]);} the } the } - } in intMain () the { the intN,m,i,mn,j,k,bb,ee; AboutN=read (); M=read (); k=read (); theBb=read (); Ee=read (); bb++;ee++; the for(i=1; i<=m;i++) the { +U[i]=read (); V[i]=read (); val[i]=read (); -u[i]++; v[i]++; the }Bayimemset (head,-1,sizeof(Head)); Cnt=1; then= (k +1)*N; the for(i=0; i<=k;i++) - { - for(j=1; j<=m;j++) Addedge1 (i*n+u[j],i*n+v[j],val[j]); the if(i!=k) the { the for(j=1; j<=m;j++) {Addedge (I*n+u[j], (i+1) *n+v[j],0); Addedge (I*n+v[j], (i+1) *n+u[j],0);} the } - } the Dijkstra (BB); themn=INF; the for(i=0; i<=k;i++) Mn=min (mn,dis[i*n+EE]);94printf"%d", MN); the fclose (stdin); the fclose (stdout); the return 0;98}
Bzoj 2763: [JLOI2011] Flight Route Dijkstra, heap, shortest path, layered map