dijkstra 优先队列最短路模板

const  int inf= 1000000000;
const  int maxn= 300010;
int dist[maxn],head[maxn],tol,m,n;
struct Edge
{
         int to,next,val;
}edge[ 10*maxn];
void add( int u, int v, int w)
{
        edge[tol].to=v;
        edge[tol].next=head[u];
        edge[tol].val=w;
        head[u]=tol++;
}
struct Node
{
         int id,dist;
        Node( int a= 0, int b= 0):id(a),dist(b){}
         bool  operator < ( const Node &b)  const
        {
                 return dist>b.dist;
        }
};
void fun( int st)
{
         int i,j,u,v;
        priority_queue<Node> q;
        q.push(Node(st, 0));
         for(i= 1;i<=n;i++)dist[i]=inf;
        dist[st]= 0;
         while(!q.empty())
        {
                Node ret=q.top();q.pop();
                u=ret.id;
                 if(dist[u]<ret.dist) continue;
                 for(i=head[u];i!=- 1;i=edge[i].next)
                {
                        v=edge[i].to;
                         if(dist[v]>dist[u]+edge[i].val)
                        {
                                dist[v]=dist[u]+edge[i].val;
                                q.push(Node(v,dist[v]));
                        }
                }
        }
}
my code

你可能感兴趣的:(dijkstra)