1579: [Usaco2009 feb]revamping Trails Road Upgrade time limit:10 Sec Memory limit:64 MB
submit:1573 solved:428
[Submit] [Status] [Discuss] Description
Every day, farmer John needs to go through some roads to check cattle in the barn N. On the farm there are M (1<=m<=50,000) Two-way dirt roads, numbered 1. M. Road I connects the bullpen p1_i and p2_i (1 <= p1_i <= N; 1 <= p2_i<= N). John needs t_i (1 <= t_i <= 1,000,000) time unit with road I from p1_i to p2_i or from p2_i to p1_i he wants to update some of the pathways to reduce the time spent on the road every day. Specifically, he wanted to update K (1 <= k <= 20) to reduce their required time by 0. Help FJ Choose which paths need to be updated to make the time from 1 to n as small as possible.
Input
* First line: Three spaces separated by number: N, M, and K * 2nd. M+1 Line: Line i+1 has three spaces separated by: P1_i, P2_i, and T_i
Output
* First line: The shortest path length after the most K-route is updated.
Sample Input4 4 1
1 2 10
2 4 10
1 3 1
3 4 100
Sample Output1
HINT
K is 1; Updating the road 3->4 reduced the time from 3 to 4 from 100 to 0. The latest shortest circuit is 1->3->4, total time is 1 units. n<=10000
Source
Gold
Exercises
Hierarchical Graph +dijkstra+ Heap optimization
The adjacent two layers are connected directly, and each layer is connected. Then start from 1 to run the shortest way. Finally, the last value of each layer is taken to a minimum.
1#include <bits/stdc++.h>2 using namespacestd;3 #defineMAXN 100104 #defineMAXM 500105 #defineINF 1e96 structnode7 {8 intEnd,value,next;9}edge[ +*4*MAXM];Ten intcnt,head[ +*maxn],n,dis[ +*maxn],heap[ +*maxn],pos[ +*MAXN],SIZE,U[MAXM],V[MAXM],VAL[MAXM]; One voidAddedge (intBbintEeintVV) A { -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; AboutN=read (); M=read (); k=read (); the for(i=1; i<=m;i++) the { theU[i]=read (); V[i]=read (); val[i]=read (); + } -memset (head,-1,sizeof(Head)); Cnt=1; then= (k +1)*N;Bayi for(i=0; i<=k;i++) the { the for(j=1; j<=m;j++) Addedge1 (i*n+u[j],i*n+v[j],val[j]); - if(i!=k) - { 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 } theDijkstra1); -mn=INF; the for(i=0; i<=k;i++) Mn=min (mn,dis[i*n+n]); theprintf"%d", MN); the fclose (stdin);94 fclose (stdout); the return 0; the}
Bzoj 1579: [Usaco2009 feb]revamping Trails Road Upgrade Dijkstra, heap, layered map