1. 程式人生 > >最短路~dijkstra堆優化模板

最短路~dijkstra堆優化模板

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<memory.h>
#include<cmath>
#include<queue>
#define oo 2147483647
using namespace std;
 
 int v[n];
 bool vis[n];
 int dis[n];
 int num;
 struct my
 {int x,y,l,ne;}e[m];
 queue<int>q;
 void put(int x,int y,int l)
 { 
   num++;
   e[num].x=x;
   e[num].y=y;
   e[num].l=l;
   e[num].ne=v[x];
   v[x]=num;
  	}
struct QQ{int x,len;};
   struct cmp
   {
	bool operator()(QQ a,QQ b){
		return a.len>b.len;
	}
   };
 priority_queue<QQ,vector<QQ>,cmp>q;
  void dij(int start)
  { 
	memset(dis,0x3f,sizeof(dis));
	memset(vis,0,sizeof(vis));
	dis[start]=0;
	q.push((QQ){start,0});
	int x;
	int y;
	while(!q.empty())
	{
		x=q.top().x;
		q.pop();
		if(vis[x])continue;vis[x]=1;
		for(int i=v[x];i;i=e[i].ne)
		{
			y=e[i].y;
			if(dis[y]>dis[x]+e[i].l)
			{
				dis[y]=dis[x]+e[i].l;
				q.push((QQ){y,dis[y]});
			}
		}
	}
      }