-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathdijkstra.cpp
More file actions
68 lines (66 loc) · 1.37 KB
/
dijkstra.cpp
File metadata and controls
68 lines (66 loc) · 1.37 KB
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
#include<iostream>
#include<queue>
using namespace std;
int arr[5][5]={{0,10,0,5,0},{0,0,1,2,0},{0,0,0,0,4},{0,3,9,0,2},{7,0,6,0,0}};
priority_queue< <int>,vector<int>,greater<int> > q;
int main()
{
int v;
cout<<"enter njumber of vertices\n";
cin>>v;
int key[v],i,j;
bool visited[v];
for(i=0;i<v;i++)
{
key[i]=INT_MAX;
visited[i]=false;
}
key[0]=0;
int p=1;
int min=key[0];
while(p)
{
int l=0;
for(i=0;i<v;i++)
{
cout<<key[i]<<" ";
if(visited[i]==false)
{
l=1;
}
}
cout<<"\n";
if(l==0)
{
p=0;
break;
}
for(i=0;i<v;i++)
{
if(key[i]<min && visited[i]==false)
{
min=i;
}
}
cout<<min<<" L "<<endl;
visited[min]=true;
for(j=0;j<v;j++)
{
if(arr[min][j]!=0)
{
if(key[j]>key[min]+arr[min][j])
{
key[j]=key[min]+arr[min][j];
}
if(key[j]<min)
{
min=key[j];
}
}
}
}
for(i=1;i<v;i++)
{
cout<<key[i]<<endl;
}
}