bfs+ linked List
Code changed from a blog
1#include <stdio.h>2#include <iostream>3#include <algorithm>4#include <math.h>5#include <string.h>6#include <string>7#include <map>8#include <Set>9#include <vector>Ten#include <queue> One using namespacestd; A Const intMAXN = 2e5+5; - Const intINF =0x3f3f3f3f; - Const intA=INF; the Const intb=1; -typedefLong LongLL; - inth[maxn],e; - LL DIS[MAXN]; + BOOLVIS[MAXN]; - intstart; + A structEdge at { - intv,nxt,w; -} e[maxn<<1]; - - voidInit () - { inE=0; -memset (h,-1,sizeofh); tomemset (Vis,false,sizeofvis); + } - the voidAddintUintVintW) * { $E[E].V =v;Panax NotoginsengE[E].W =W; -E[E].NXT =H[u]; theH[u] = e++; + } A the structQnode + { - intv,c; $Qnode (int_v,int_c) $ { -v =_v; -C=_c; the } - BOOL operator< (ConstQnode &a)ConstWuyi { the returnC>A.C; - } Wu }; - About voidDijkstraintN) $ { -memset (Dis,inf,sizeofdis); -Priority_queue<qnode>Q; -dis[1] =0; AQ.push (Qnode (1,0)); + while(!q.empty ()) the { -Qnode tmp =q.top (); $ Q.pop (); the intU =tmp.v; the if(Vis[u])Continue; theVis[u] =true; the for(intI=h[u]; ~i; I=e[i].nxt) - { in intv =e[i].v; the intW =E[I].W; the if(Vis[v])Continue; About if(dis[u]+w<Dis[v]) the { theDIS[V] = dis[u]+W; the Q.push (Qnode (v,dis[v)); + } - } the }Bayi } the the voidBFS (intn,ll val) - { - Set<int>TA,TB; thequeue<int>Q; the Q.push (start); theDis[start] =0, dis[n] =INF; the for(intI=1; i<=n; i++){ - if(I==start)Continue; the Ta.insert (i); the } the while(!q.empty ())94 { the intU =Q.front (); the Q.pop (); the for(intI=h[u]; ~i; I=e[i].nxt)98 { About intv =e[i].v; - if(!ta.count (v))Continue;101 Ta.erase (v);102 Tb.insert (v);103 }104 for(Set<int>::iterator It=ta.begin (); It!=ta.end (); it++) the {106Q.push (*it);107Dis[*it] = Dis[u] +Val;108 }109 Ta.swap (TB); the tb.clear ();111 } the }113 the intMain () the { the intn,m,t;117 intu,v;118 while(SCANF ("%d", &t)! =EOF)119 { - while(t--)121 {122scanf"%d%d",&n,&M);123 init ();124 BOOLFlag =false; the for(intI=0; i<m; i++)126 {127scanf"%d%d",&u,&v); - Add (u,v,a);129 Add (v,u,a); the 131 } thescanf"%d",&start);133 Dijkstra (N);134 BFS (n,b);135 BOOLfirst=0;136 for(intI=1; i<=n;i++)137 {138 if(I==start)Continue;139 if(first) printf (" "); $printf"%d", Dis[i]);141first=1;142 }143printf"\ n");144 }145 }146 return 0;147 }148 149 Max /*151 the 1153 2 0154 1155 156 */
ICPC Dalian Station Network race 1009 Short Circuit