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}