forked from TECHOUS/AlgoHeist
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDIJKSTRA.cpp
More file actions
70 lines (64 loc) · 1.55 KB
/
DIJKSTRA.cpp
File metadata and controls
70 lines (64 loc) · 1.55 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;
int dij(int source, int v, vector<pair<int,int>> adj[]){
bool boolean[v];
memset(boolean, false, sizeof(boolean));
int track[v];
for (int i = 0; i < v; ++i)
{
track[i]=10000;
}
track[source] = 0;
set<pair<int,int>> s;
s.insert(make_pair(0,source));
while(!s.empty()){
int dis = (*s.begin()).first;
int vert = (*s.begin()).second;
s.erase(*s.begin());
if(boolean[vert])
continue;
else
boolean[vert]=true;
for (int i = 0; i < adj[vert].size(); ++i)
{
if( track[adj[vert][i].first] > track[vert] + adj[vert][i].second ) {
track[adj[vert][i].first] = track[vert] + adj[vert][i].second;
s.insert(make_pair(track[adj[vert][i].first], adj[vert][i].first));
}
}
}
cout<<endl;
for (int i = 0; i < v; ++i)
{
cout<<track[i]<<" ";
}
}
int main(){
int e,v,u,w,source,weight;
cout << "enter number of edges: ";
cin >> e;
cout << "enter number of vertices: ";
cin >> v;
cout << "tell the vertices between which there exist an edge and corressponding weights\n";
vector<pair<int,int>> adj[v];
for (int i = 0; i < e; ++i)
{
cin >> u >> w >> weight;
adj[u].push_back(make_pair(w,weight));
adj[w].push_back(make_pair(u,weight));
}
cout << "enter the source node: ";
cin >> source;
cout << endl << "Adjacency list" << endl;
for (int i = 0; i < v; ++i)
{
cout << i << " -> ";
for (int j = 0; j < adj[i].size(); ++j)
{
cout<<adj[i][j].first<<","<<adj[i][j].second<<"->";
}
cout << endl;
}
cout << endl;
dij(source,v,adj);
}