Dijkstra模板

  • 优先队列+堆优化+链式前向星
  • O(n^2) n<=5000

 

 1 inline void add(int u,int v,int w){
 2     e[++cnt].to=v;
 3     e[cnt].w=w;
 4     e[cnt].nxt=head[u];
 5     head[u]=cnt;
 6 }
 7 inline void dj(){
 8     priority_queue<hnode>q;
 9     For(i,1,n) dis[i]=0x3f3f3f;
10     dis[1]=0;12     q.push((hnode){0,1});
13     while(!q.empty()){
14         hnode x=q.top();q.pop();int u=x.u;
15         if(vis[u]) continue;
16         vis[u]=true;
17         for(int i=head[u];i;i=e[i].nxt){
18             int v=e[i].to;
19             if(dis[v]>dis[u]+e[i].w){
20                 dis[v]=dis[u]+e[i].w;
21                 q.push((hnode){dis[v],v});
22             }
23         }
24     }
25 }

 

posted @ 2019-08-07 16:34  song_jian  阅读(139)  评论(0编辑  收藏  举报