http://acm.hdu.edu.cn/showproblem.php?pid=2544
1 /*2 use pair instead of structure3 */4 5#include <iostream>6#include <cstdio>7#include <queue>8#include <vector>9 using namespacestd;Ten Const intNi =10000; One Const intINF =1<< -; A -typedef pair<int,int>PA; - the intDis[ni],n;//part of Dis using 1-n - -vector<pair<int,int> >Eg[ni]; - + voidDijkstra (ints) - { + inti,j; A for(i=0; i<=n;i++)//To go to n atDis[i] =INF; -Priority_queue<pa> Q;//Priority queue: small-top heap -Dis[s] =0; - Q.push (Make_pair (S,dis[s)); - - while(!q.empty ()) in { -PA x =q.top (); to Q.pop (); + intW =X.first; - for(j =0; J<eg[w].size (); j + +)//Traverse all adjacency points of x the { *Pa y = eg[w][j];//y is the adjacency point of X $ intU =Y.first;Panax Notoginseng if(dis[u]>x.second+Y.second) - { theDis[u] = x.second+Y.second; + Q.push (Make_pair (U,dis[u)); A } the } + } - $ } $ - - intMain () the { - intM,a,b,d;//Number of relationshipsWuyi while(cin>>n>>m,m+N) the { - for(inti =0; i<=n;i++) WuEg[i].clear ();//Initialize - while(m--) About { $Cin>>a>>b>>D; - Eg[a].push_back (Make_pair (b,d)); - Eg[b].push_back (Make_pair (a,d)); - } A +Dijkstra (1); the if(dis[n]!=INF) -cout<<dis[n]<<Endl; $ Else thecout<<"-1\n"; the } the the return 0; -}
HDU 2544 (Simple shortest circuit)