This problem as a template problem, the solution is many ...
Recently around the people are on the map, I feel good spicy chicken, will only follow the follow-up study.
Only SPFA for the moment.
SPFA (adjacency table version.) Can also be written as a pro-matrix map, but the topic may give parallel edges, so pay attention to find the smallest side of the storage, but also to determine whether a point more than once into the queue) honestly think that SPFA seems to be just a queue optimized search the same.
1#include <stdio.h>2#include <algorithm>3#include <iostream>4#include <string.h>5#include <utility>6#include <vector>7#include <queue>8 #defineMAXX 100109 using namespacestd;Ten One Const intN = -; A Const intINF =0x3f3f3f3f; - - thetypedefstructSion - { - intdis; - intNXP; + }sion; - + intDic[n]; A BOOLVis[n]; atVector<sion>Li[n]; - - - intSPFA (int&s,int&t) - { -queue<int>Q; in Q.push (s); -Vis[s] =1; toDic[s] =0; + while(!q.empty ()) - { the intPre =Q.front (); * Q.pop (); $ for(inti =0; I < li[pre].size (); i++)Panax Notoginseng { - //cout << Dic[li[pre][i].nxp] << "~"; the if(Dic[li[pre][i].nxp] > Dic[pre] +Li[pre][i].dis) + { ADIC[LI[PRE][I].NXP] = Dic[pre] +Li[pre][i].dis; the Q.push (LI[PRE][I].NXP); + //cout << DIC[LI[PRE][I].NXP] << "!"; - } $ } $ - } - returnDic[t]; the } - Wuyi voidInit () the { -memset (Vis,false,sizeof(Vis)); Wumemset (Dic,inf,sizeof(DIC)); - for(inti =0; i < N; i++) About li[i].clear (); $ return ; - } - - intMain () A { + intN, M; the ints, t; - while(Cin >> N >>m) $ { the init (); the sion A; the intx; the for(inti =0; I < m; i++) - { inscanf"%d%d%d", &x, &A.NXP, &A.dis); the Li[x].push_back (a); the inttemp =A.nxp; AboutA.NXP =x; the Li[temp].push_back (a); the } thescanf"%d%d", &s, &t); + inty =SPFA (S, t); - if(Y >=INF) the {Bayiprintf"-1\n"); the } the Elseprintf"%d\n", y); - } -}
HDU 1874 Spfa/bellmanford/dijkstra/floyd