Test instructions: Chinese title
Idea: SPFA+SLF optimization. Please poke me for details about SPFA.
1#include <bits/stdc++.h>2 using namespacestd;3 Const intn= the, inf=0x7f7f7f7f;4 intN, M;5 BOOLVis[n];//whether the tag is in the queue6 intDest[n];//Path Length7 intMapp[n][n];8 9 Ten One intSPFA (intSintE//find the shortest path A { -memset (dest,0x7f,sizeof(dest)); -dest[s]=0; thevis[s]=1; -deque<int>que; - Que.push_back (s); - while(!que.empty ()) + { - inttmp=Que.front (); + Que.pop_front (); Avis[tmp]=0; at for(intI=1; i<=n; i++) - { - if(Mapp[tmp][i] && dest[tmp]+mapp[tmp][i]<Dest[i]) - { -dest[i]=dest[tmp]+Mapp[tmp][i]; - if(!vis[i])//once it has been updated, it must enter the queue in { -vis[i]=1; to if(Que.empty ()) que.push_back (i);//SLF optimization, road short priority put front + Else - { the if(Dest[que.front ()]>Dest[i]) * Que.push_front (i); $ ElsePanax Notoginseng Que.push_back (i); - } the } + } A } the } + returnDest[e]; - } $ $ - - the intMain () - {Wuyi //freopen ("Input.txt", "R", stdin); the intA, B, C; - while(SCANF ("%d%d", &n, &m), n+m) Wu { -memset (Vis,0,sizeof(Vis)); Aboutmemset (MAPP,0,sizeof(MAPP)); $ - for(intI=0; i<m; i++) - { -scanf"%d%d%d",&a,&b,&c); Amapp[a][b]=mapp[b][a]=C; + } theprintf"%d\n", SPFA (1, N)); - } $ the return 0; the}
AC Code
HDU 2544 Shortest Way (single source to short circuit, Spfa,dijkstra,bellman-ford)