-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathDijstra's Algorithm.cpp
More file actions
70 lines (67 loc) · 2 KB
/
Dijstra's Algorithm.cpp
File metadata and controls
70 lines (67 loc) · 2 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
69
70
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
# define INF 0x3f3f3f3f
typedef pair<int, int> pi;
vector<pair<ll,ll > > adj[1005];
int dist[1005];
void bfs(int** edges,int x,int n) //x=starting vertex n=total vertex
{
int s=0;
for(int i=0;i<n;i++)
{
dist[i]=INF;
}
priority_queue<pair<int,int > > pq;
pq.push(make_pair(0,x));//distance is zero and x is source (it
// has to be done this way to keep the vertices
// sorted distance (distance must be first item
// in pair)
dist[x]=0;
while(!pq.empty())
{
int m=pq.top().second;//source x is taken out
//cout<< m <<" stand "<<endl;
pq.pop();
for(int j=0;j<n;j++)
{
//cout<<j<<endl;
//cout<<dist[j]<<" adj ka dist--> current vertex ka dist"<<dist[m]<<" weight-> "<<edges[m][j]<<endl;
if(edges[m][j]>0)
{
if(dist[j]> dist[m] + edges[m][j] )
{
//first: is child vertex and second: is weight on adj edge to m
dist[j] = dist[m] + edges[m][j] ;
//cout<<"entered "<<dist[j]<<" --> "<<dist[m]<<" "<<edges[m][j]<<endl;
pq.push(make_pair(dist[j],j));
}
}
}
}
for(int i=0;i<n;i++)
cout<< i<<" "<<dist[i]<<endl;
}
int main()
{
ll n,e,x,y,z; //n=nodes e=edges
cin>>n>>e; //entering no. of node and edges
int** edges=new int*[n+2];
for(int i=0;i<=n;i++)
{
edges[i]=new int[n+2];
for(int j=0;j<=n;j++)
{
edges[i][j]=0;
}
}
for(int i=1;i<=e;i++)
{
cin>>x>>y>>z; //x-->y edges connection...corresponding z weight
edges[x][y]=z; //undirected graph
edges[y][x]=z;
}
bfs(edges,0,n);//starting vertex and no. of vertices
return 0;
}