-
Notifications
You must be signed in to change notification settings - Fork 1
/
DijkstraAlgorithm.cpp
52 lines (47 loc) · 1.7 KB
/
DijkstraAlgorithm.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <stdio.h>
#include <vector>
#include <queue>
const int Inf = 30000;
int main(){
int n, s, l;
//printf("Initiate number of vertexes: ");
scanf("%d ", &n);
//printf("Initiate starting vertex: "); //s in [1;n]
scanf("%d ", &s);
//printf("Initiate last vertex: "); //l in [1;n]
scanf("%d ", &l);
s--;
l--;
//making up the matrix of vertexses
std::vector <std::vector <int>> matrix (n, std::vector <int> (n));
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
//printf("Set the value of %d %d", i, j);
scanf("%d", &matrix[i][j]);
}
}
std::vector <int> distances (n, Inf); //distances[x] is distance from s to x
distances[s] = 0; //distance from[s] to s obviously equals to 0
std::priority_queue <std::pair <int, int>> queue; //queue{{len, ver}}
queue.push(std::make_pair(0, s));
while(!queue.empty()){ //while we haven't visited to last vertex (aka queue isnt empty)
int len = -queue.top().first;
int v = queue.top().second;
queue.pop(); //clearing the queue
if (len > distances[v]) //if INF - skip iteration
continue;
for (int i = 0; i < n; i++){
int vert = i;
int len_of_vert = matrix[v][i]; // if where's a way to get to vertex faster than distances[vert], then it's a new distance
if (distances[vert] > distances[v] + len_of_vert){
distances[vert] = distances[v] + len_of_vert;
queue.push(std::make_pair(-distances[vert], vert));
}
}
}
if (distances[l] == Inf){
printf("Unable to get there");
}else{
printf("%d", distances[l]);
}
}