# The Dijkstra algorithm using C ++ STL

I implements Dijkstra's algorithm using C ++ STL.

not e (number of vertices and number of edges)
followed by e edge lines and their weights w
followed by you and v the shortest path between which is

An integer representing the shortest path between you and v

`adj[][]` : representation of the list of contiguity of the graph

`Cost[][]` : weights associated with each vertex

I implement my own priority queue, which prioritizes the summits according to their `dist` values

Here are the functions I've implemented:

1. `distance (vector <vector > & adj, vector <vector > & cost, int s, int t)` the main logic of the algorithm is implemented here

2. `vector makequeue (vector <vector> adj, vector dist)` returns an initial data structure min-pile of vertices (ranked according to the dist values)

3. `int extract_min (vector & H, vector dist)` returns and removes the minimum element of the min-heap

4. `void diminution_key (vector & H, int i, int key, vector dist)` takes the arguments as: heap, index of the element for which the key is to be changed (i), the value of the key and the remote table

5. `void min_heapify (vector & H, int i, vector dist)`

``````#understand
#understand
#understand
#understand

using std :: vector;
using std :: cout;

int heapsize;

int parent (int i) {
if (i% 2 == 0) returns (i / 2) - 1;
returns i / 2;
}

void min_heapify (vector & H, int i, vector dist) {
int l = (2 * i) + 1;
int r = (2 * i) + 2;
int the smallest = i;
if (l <heapsize && dist[H[l]]<dist[H[i]]) the smallest = 1;
if (r <stack size && dist[H[r]]<dist[H[i]]) the smallest = r;
if (the smallest! = i) {
std :: swap (H[i], H[smallest])
min_heapify (H, the smallest, dist);
}
}

void diminution_key (vector  & H, int i, int key, vector dist) {
while I < heapsize && i > 0 && dist[H[parent(i)]]> dist[H[i]]) {
std :: swap (H[i], H[parent(i)])
i = parent (i);
}
}

int extract_min (vector & H, vector dist) {
if (size> = 1) {
int min = H;
H = H[heapsize - 1];
H[heapsize - 1] = -1;
heap size -;
min_heapify (H, 0, dist);
return min;
}
}

vector makequeue (vector <vector> adj, vector dist) {
vector H;
heap size = adj.size ();
for (int i = 0; i < adj.size(); i ++) H.push_back(i);
for (int i = H.size() / 2; i >= 0; I --) {
min_heapify (H, i, dist);
}
returns H;
}

int distance (vector <vector > & adj, vector <vector > & cost, int s, int t) {
vector  dist (adj.size (), std :: numeric_limits:: max ());
dist [s] = 0;
vector H = makequeue (adj, dist);
int u;
while (heapsize! = 0) {
u = extract_min (H, dist);
for (int i = 0; i <adj[u].Cut(); i ++) {
if ((dist[u] ! = std :: numeric_limits:: max ()) && (dist[adj[adj[adj[adj[u][i]]> dist[u] + cost[u][i])) {
vector:: iterator it = find (H.begin (), H.begin () + heapsize, adj[u][i])
}
}
}

if (dist
if not return dist
}

int main () {
int n, m;
std :: cin >> n >> m;
vector <vector > adj (n, vector());
vector <vector > cost (n, vector());
for (int i = 0; i < m; i++) {
int x, y, w;
std::cin >> x >> y >> w;
adj[x - 1].push_back (y - 1);
Cost[x - 1].push_back (w);
}
int s, t;
std :: cin >> s >> t;
s--, t--;
std :: cout << distance (adj, cost, s, t);
}
``````

10 9

1 2 1

2 3 1

3 4 1

4 5 1

5 6 1

6 7 1

7 8 1

8 9 1

9 10 1

1 10

## Exit

9

the platform on which I wish to submit the code gives a wrong answer for a particular test case, I have not been able to determine the problem.

My best guess is in the `touche_diminution` run since I used a workaround here – by first doing a `find` to determine the summit index. (Something similar to what has been requested here: https://stackoverflow.com/questions/17009056/how-to-implement-ologn-decrease-key-operation-for-min-heap-based-priority-queu )

I'm not sure how to use the priority_queue container in C ++ STL, so I went with my own implementation. I would prefer that someone guide me on what is wrong with the code I wrote.