dijkstra
1 #define pa pair<int,int> 2 int n,m,tot; 3 int d[1005],head[1005]; 4 bool v[1005]; 5 struct edge{int go,next,w;}e[50005]; 6 void insert(int x,int y,int z) 7 { 8 e[++tot].go=y;e[tot].w=z;e[tot].next=head[x];head[x]=tot; 9 } 10 void dijkstra() 11 { 12 priority_queue<pa,vector<pa>,greater<pa> >q; 13 for(int i=1;i<=n;i++)d[i]=inf; 14 memset(v,0,sizeof(v)); 15 d[1]=0;q.push(make_pair(0,1)); 16 while(!q.empty()) 17 { 18 int x=q.top().second;q.pop(); 19 if(v[x])continue;v[x]=1; 20 for(int i=head[x],y;i;i=e[i].next) 21 if(d[x]+e[i].w<d[y=e[i].go]) 22 { 23 d[y]=d[x]+e[i].w; 24 q.push(make_pair(d[y],y)); 25 } 26 27 } 28 }