-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dijkstra's_Algorithm.cpp
132 lines (112 loc) · 2.9 KB
/
Dijkstra's_Algorithm.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ff first
#define ss second
#define sz(x) (int) x.size()
#define endl '\n'
#define pb push_back
#define all(v) v.begin(), v.end()
#define YES printf("YES\n")
#define Yes printf("Yes\n")
#define NO printf("NO\n")
#define No printf("No\n")
#define mem(a) memset(a , 0 ,sizeof a)
#define memn(a) memset(a , -1 ,sizeof a)
const int lim = 1048576;
const int M = 1e9 + 7;
const int Inf = (int)2e9 + 5;
const ll Lnf = (ll)2e18 + 5;
const int N = 5e5 + 5;
const int NN = 1e6 + 5;
#define error(args...) {vector<string>_v=split(#args,',');err(_v.begin(),args);cout<<endl;}
vector<string> split(const string &s, char c) {vector<string>v; stringstream ss(s); string x; while (getline(ss, x, c))v.emplace_back(x); return move(v);} void err(vector<string>::iterator it) {}
template<typename T, typename... Args>void err(vector<string>::iterator it, T a, Args...args) {cout << it->substr((*it)[0] == ' ', it->length()) << " = " << a << " "; err(++it, args...);}
std::vector<pair<int, int>>g[N];
std::vector<int>vis(N, 0);
std::vector<int>dist(N, Inf);
int n, m;
void dijkstra(int source)
{
set<pair<int, int>>st;
st.insert({0, source});
dist[source]=0;
while (st.size())
{
auto node = *st.begin();
int v = node.ss;
int dist_v = node.ff;
st.erase(st.begin());
//error(v,dist_v);
if (vis[v])continue;
vis[v] = 1;
for (auto child : g[v])
{
int child_v=child.ff;
int wt=child.ss;
if(dist[v]+wt<dist[child_v])
{
dist[child_v]=dist[v]+wt;
st.insert({dist[child_v],child_v});
}
}
}
for (int i =1; i <=n; ++i)
{
cout<<"Node :"<<i<<" distance "<<dist[i]<<endl;
}
}
// void dijkstra(int source)
// {
// priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>pq;
// pq.push({0, source});
// while (!pq.empty())
// {
// pair<int, int>p = pq.top();
// pq.pop();
// int v = p.ss;
// int dist_v = p.ff;
// if (vis[v])continue;
// dist[v] = dist_v;
// vis[v] = 1;
// for (auto child : g[v])
// {
// int child_v = child.ff;
// int wt = child.ss;
// if (vis[child_v])continue;
// pq.push({dist[v] + wt, child_v});
// }
// }
// for (int i = 1; i <= n; ++i)
// {
// cout << "node " << i << " distance :" << dist[i] << endl;
// }
// }
int solve()
{
//int n, m;
cin >> n >> m;
for (int i = 0; i < m; ++i)
{
int x, y, wt;
cin >> x >> y >> wt;
g[x].push_back({y, wt});
g[y].push_back({x, wt});
}
clock_t tStart = clock();
dijkstra(1);
printf("Time taken: %.2fs\n", (double)(clock() - tStart) / CLOCKS_PER_SEC);
return 0;
//error();
}
int main() {
//ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int test = 1, tc = 0;
//cin >> test;
//scanf("%d", &test);
while (test--) {
//printf("Case %d: ", ++tc);
solve();
}
return 0;
}