-
Notifications
You must be signed in to change notification settings - Fork 368
/
Dijkstra.cpp
70 lines (59 loc) · 1.5 KB
/
Dijkstra.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include<bits/stdc++.h>
using namespace std ;
int cost[100][100] , n ;
int getMin(int dist[] , bool visited[]){
int key = 0 ;
int min = INT_MAX ;
for(int i=0;i < n ; i++){
if(!visited[i] && dist[i]<min){
min = dist[i] ;
key = i ;
}
}
return key ;
}
void display(int dist[] , int par[] ){
for(int i =0 ;i < n ;i++){
int temp = par[i] ;
cout<<i << " <- " ;
while(temp!=-1)
{
cout<< temp << " <- " ;
temp = par[temp] ;
}
cout<<endl;
cout<<"::::Distance = " << dist[i] ;
cout<<endl;
}
}
void dijkstra(int src ){
int par[100] , dist[100] ;
bool visited[100] ={0} ;
fill(dist , dist+n , INT_MAX ) ;
dist[src] =0 ;
par[src] =-1 ;
for(int g = 0 ;g<n-1 ; g++){
int u = getMin( dist , visited ) ;
visited[u] = true ;
cout<< " min = " << u <<endl;
for(int v =0 ; v< n ;v++){
if(!visited[v] && (dist[u]+cost[u][v]) < dist[v] && cost[u][v]!=9999)
{
par[v] = u ;
dist[v] = dist[u] + cost[u][v] ;
}
}
}
display(dist , par) ;
}
int main(void) {
cout<<"ENTER THE SIZE OF MATRIX : " ;
cin>>n ;
cout<<"ENTER THE COST OF MATRIX : \n" ;
for(int i = 0 ;i < n ; i++){
for(int j = 0 ; j< n ; j++)cin>>cost[i][j] ;
}
int src ;
cout<<"\nENTER START NODE: " ; cin>>src ;
dijkstra(src) ;
}