-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdijkstra.cpp
More file actions
40 lines (36 loc) · 977 Bytes
/
dijkstra.cpp
File metadata and controls
40 lines (36 loc) · 977 Bytes
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
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 1;
int n,m;
vector<pair<int,int> > adj[N];
priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > q;
int dist[N];
bool visited[N];
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> m;
for(int i = 0;i < m;i++)
{
int a,b,d;
cin >> a >> b >> d;
adj[a].push_back({d,b});
adj[b].push_back({d,a});
}
for(int i = 1;i <= n;i++) dist[i] = INT_MAX;
q.push({0,1});
dist[1] = 0;
while(!q.empty())
{
int u = q.top().second;
q.pop();
if(visited[u]) continue;
visited[u] = true;
for(int i = 0;i < adj[u].size();i++)
{
int w = adj[u][i].first,v = adj[u][i].second;
if(dist[u]+w<dist[v]) dist[v] = dist[u]+w,q.push({dist[v],v});
}
}
for(int i = 1;i <= n;i++) cout << dist[i] << '\n';
}