-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDijkstra_algo.cpp
208 lines (179 loc) · 3.62 KB
/
Dijkstra_algo.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
maneesh(maik)
*/
// Dijkstra algo
// The shortest path for undirected
// you can make this directed
#include<iostream>
#include<algorithm>
#include<string>
#include<bitset>
#include<deque>
#include<iterator>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<vector>
using namespace std;
#define ll long long int
#define pb push_back
#define pf push_front
#define pii pair<ll,ll>
#define mp make_pair
#define M 1000000007
#define fastio ios_base::sync_with_stdio(false);
struct comp
{
bool operator()(const pair<int , int> &a , const pair<int , int> &b)
{
return a.first > b.first;
}
};
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n,m;
cin>>n>>m;
vector<ll>G[n+2];
vector<ll>cost[n+2];
ll i,x,y,w,mx=0;
for(i=0; i<m; i++)
{
cin>>x>>y>>w;
G[x].pb(y);
cost[x].pb(w);
G[y].pb(x);
cost[y].pb(w);
}
/* for(i=1; i<=n; i++)
{
cout<<i<<" ";
for(int j=0; j<G[i].size(); j++)
{
cout<<"("<<G[i][j]<<", "<<cost[i][j]<<") ";
}
cout<<endl;
} cout<<endl; */
ll dist[n+2];
for(i=1; i<=n; i++)
{
dist[i]=1000000000;
}
priority_queue<pii, vector<pii>, comp>pq;
ll source;
cin>>source;
dist[source]=0;
pq.push(mp(0, source));
while(!pq.empty())
{
ll node=pq.top().second;
pq.pop();
for(i=0; i<G[node].size(); i++)
{
ll it=G[node][i];
if(dist[it]>dist[node]+cost[node][i])
{
dist[it]=dist[node]+cost[node][i];
pq.push(mp(dist[it], it));
}
}
}
// distance from source to other
for(i=1; i<=n; i++)
{
cout<<dist[i]<<" ";
}
return 0;
}
// other method of Dijkstra
/*
struct data
{
ll nod;
ll val;
};
// comperator function for priority queue
struct comp
{
bool operator()(data const& s1, data const& s2)
{
return s1.val > s2.val;
}
};
int main()
{
fastio;
ll n,m;
cin>>n>>m;
ll i,x,y,w,mx=0;
vector<data>v[n+1];
data z1,z2;
for(i=0; i<m; i++)
{
cin>>x>>y>>w;
mx=mx+w;
z1.nod=y; z1.val=w;
v[x].pb(z1);
z2.nod=x; z2.val=w;
v[y].pb(z2);
}
mx=mx+100;
ll vis[n+1],dist[n+1];
for(i=1; i<=n; i++)
{
vis[i]=0;
dist[i]=mx;
}
// for(i=1; i<=n; i++)
// {
// cout<<i<<" ";
// for(int j=0; j<v[i].size(); j++)
// {
// cout<<v[i][j].nod<<" ";
// }
// cout<<endl;
// }
// source node
ll s;
cin>>s;
// define priority queue of structure
priority_queue<data, vector<data>, comp>pq;
z1.nod=s; z1.val=0;
pq.push(z1);
vis[s]=1;
dist[s]=0;
while(!pq.empty())
{
z1=pq.top();
ll p=z1.nod; //cout<<p<<" ";
vis[p]=1;
pq.pop();
// traverse in the adjacency list of p
for(i=0; i<v[p].size(); i++)
{
if(dist[v[p][i].nod] > dist[p]+v[p][i].val )
{
dist[v[p][i].nod]=dist[p]+v[p][i].val;
}
// if not visited push it in to priority_queue
if(vis[v[p][i].nod]==0)
{
z2.nod=v[p][i].nod; z2.val=v[p][i].val;
pq.push(z2);
}
}
}
//cout<<endl;
// Shortest distance from source to all other nodes
for(i=1; i<=n; i++)
{
cout<<dist[i]<<" ";
}
return 0;
}
*/