Shortest template (Dijkstra)

Shortest

The priority_queue <pair < int , int >> que; // Frist the inverse of the length of the edge in place, second is the index

 

void the Add ( int X, int Y, int Z) // adjacency matrix, x is the starting point of this edge, y is the end, z is a length 
{ 
    Ver [ ++ TOT] = Y, Edge [TOT] = Z; 
    Next [TOT] = head [X], head [X] = TOT; 
}

 

 

void Dijkstra ( int origin) // find the shortest length of each origin point 
{    
    Memset (D, 9999999 , the sizeof (D)); 
    Memset (VIS, 0 , the sizeof (VIS)); 
    D [origin] = 0 ; 
    que. Push (the make_pair ( 0 , Origin));
     the while (que.size ()) 
    { 
        int X = que.top () SECOND;. 
        que.pop (); 
        IF (VIS [X] == . 1 ) Continue ; 
        VIS [ X] = . 1 ; 

        for ( int I = head [X]; I; I =next[i])
        {
            int y=ver[i];
            int z=edge[i];
            if(d[y]>d[x]+z)
            {
                d[y]=d[x]+z;
                que.push(make_pair(-d[y],y));
            }
        }
    }
}

 

Guess you like

Origin www.cnblogs.com/jrfr/p/11280097.html