1void dijkstra(int s) {
2 priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > pq;
3 for (int i=0; i<N; i++) dist[i] = INF;
4 dist[s] = 0;
5 pq.push(make_pair(0, s));
6 while (!pq.empty()) {
7 pair<int, int> front = pq.top();
8 pq.pop();
9 int w = front.first, u = front.second;
10 if (w > dist[u]) continue;
11 for (int i=0; i<adj[u].size(); i++) {
12 pair<int, int> v = adj[u][i];
13 if (dist[v.first] > dist[u] + v.second) {
14 dist[v.first] = dist[u] + v.second;
15 pq.push(make_pair(dist[v.first], v.first));
16 }
17 }
18 }
19}
1#include<bits/stdc++.h>
2using namespace std;
3
4int main()
5{
6 int n = 9;
7
8 int mat[9][9] = { { 100,4,100,100,100,100,100,8,100},
9 { 4,100,8,100,100,100,100,11,100},
10 {100,8,100,7,100,4,100,100,2},
11 {100,100,7,100,9,14,100,100,100},
12 {100,100,100,9,100,100,100,100,100},
13 {100,100,4,14,10,100,2,100,100},
14 {100,100,100,100,100,2,100,1,6},
15 {8,11,100,100,100,100,1,100,7},
16 {100,100,2,100,100,100,6,7,100}};
17
18 int src = 0;
19 int count = 1;
20
21 int path[n];
22 for(int i=0;i<n;i++)
23 path[i] = mat[src][i];
24
25 int visited[n] = {0};
26 visited[src] = 1;
27
28 while(count<n)
29 {
30 int minNode;
31 int minVal = 100;
32
33 for(int i=0;i<n;i++)
34 if(visited[i] == 0 && path[i]<minVal)
35 {
36 minVal = path[i];
37 minNode = i;
38 }
39
40 visited[minNode] = 1;
41
42 for(int i=0;i<n;i++)
43 if(visited[i] == 0)
44 path[i] = min(path[i],minVal+mat[minNode][i]);
45
46 count++;
47 }
48
49 path[src] = 0;
50 for(int i=0;i<n;i++)
51 cout<<src<<" -> "<<path[i]<<endl;
52
53 return(0);
54}
1#include <limits.h>
2#include <stdio.h>
3
4
5#define V 9
6
7
8int minDistance(int dist[], bool sptSet[])
9{
10
11 int min = INT_MAX, min_index;
12
13 for (int v = 0; v < V; v++)
14 if (sptSet[v] == false && dist[v] <= min)
15 min = dist[v], min_index = v;
16
17 return min_index;
18}
19
20
21void printSolution(int dist[])
22{
23 printf("Vertex \t\t Distance from Source\n");
24 for (int i = 0; i < V; i++)
25 printf("%d \t\t %d\n", i, dist[i]);
26}
27
28
29void dijkstra(int graph[V][V], int src)
30{
31 int dist[V];
32
33
34 bool sptSet[V];
35
36 for (int i = 0; i < V; i++)
37 dist[i] = INT_MAX, sptSet[i] = false;
38
39
40 dist[src] = 0;
41
42
43 for (int count = 0; count < V - 1; count++) {
44
45 int u = minDistance(dist, sptSet);
46
47
48 sptSet[u] = true;
49
50
51 for (int v = 0; v < V; v++)
52
53
54 if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX
55 && dist[u] + graph[u][v] < dist[v])
56 dist[v] = dist[u] + graph[u][v];
57 }
58
59
60 printSolution(dist);
61}
62
63
64int main()
65{
66
67 int graph[V][V] = { { 0, 4, 0, 0, 0, 0, 0, 8, 0 },
68 { 4, 0, 8, 0, 0, 0, 0, 11, 0 },
69 { 0, 8, 0, 7, 0, 4, 0, 0, 2 },
70 { 0, 0, 7, 0, 9, 14, 0, 0, 0 },
71 { 0, 0, 0, 9, 0, 10, 0, 0, 0 },
72 { 0, 0, 4, 14, 10, 0, 2, 0, 0 },
73 { 0, 0, 0, 0, 0, 2, 0, 1, 6 },
74 { 8, 11, 0, 0, 0, 0, 1, 0, 7 },
75 { 0, 0, 2, 0, 0, 0, 6, 7, 0 } };
76
77 dijkstra(graph, 0);
78
79 return 0;
80}
1const int INF = 1000000000;
2vector<vector<pair<int, int>>> adj;
3
4void dijkstra(int s, vector<int> & d, vector<int> & p) {
5 int n = adj.size();
6 d.assign(n, INF);
7 p.assign(n, -1);
8 vector<bool> u(n, false);
9
10 d[s] = 0;
11 for (int i = 0; i < n; i++) {
12 int v = -1;
13 for (int j = 0; j < n; j++) {
14 if (!u[j] && (v == -1 || d[j] < d[v]))
15 v = j;
16 }
17
18 if (d[v] == INF)
19 break;
20
21 u[v] = true;
22 for (auto edge : adj[v]) {
23 int to = edge.first;
24 int len = edge.second;
25
26 if (d[v] + len < d[to]) {
27 d[to] = d[v] + len;
28 p[to] = v;
29 }
30 }
31 }
32}
33