-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdijkstra.cpp
More file actions
47 lines (44 loc) · 854 Bytes
/
dijkstra.cpp
File metadata and controls
47 lines (44 loc) · 854 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
41
42
43
44
45
46
47
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define REP(i, a, b) for(int i =a;i<=b; i++)
const int MX = 2e5 + 3;
int mark[MX], dis[MX];
vector<pi> adj[MX];
void dijkstra(int k);
int main()
{
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int n, m;
cin>>n>>m;
REP(i, 0, m-1)
{
int x, y, z;
cin>>x>>y>>z;
adj[x].PB(MP(z, y));
adj[y].PB(MP(z, x));
}
dijkstra(0);
REP(i, 0, n) cout<<dis[i]<< endl;
return 0;
}
void dijkstra(int k)
{
set<pi> s;
s.insert(MP(k, 0));
while(s.size())
{
auto temp = (*s.begin());
s.erase(s.begin());
if(mark[temp.S]) continue;
mark[temp.S] = 1;
dis[temp.S] = temp.F;
for(auto x: adj[temp.S]) if(!mark[x.S]) s.insert(MP(temp.F+x.F, x.S));
}
}