Priority queue there will be an error with greater
http://acm.hdu.edu.cn/showproblem.php?pid=1874
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) 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 + into,p; theCin>>o>>p; - Dijkstra (o); $ if(dis[p]!=INF) thecout<<dis[p]<<Endl; the Else thecout<<"-1\n"; the } - in return 0; the } the /* About 6 6 the 1 2 2 the 3 2 4 the 1 4 5 + 2 5 2 - 3 6 3 the 5 6 3Bayi */
HDU 1874 (Simple shortest circuit)