-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcses_1671.cpp
More file actions
52 lines (46 loc) · 1.05 KB
/
cses_1671.cpp
File metadata and controls
52 lines (46 loc) · 1.05 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
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
typedef long long ll;
const int MAXN = 100087;
ll n, m, a, b, w;
vector<pair<ll, ll>> G[MAXN];
priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq;
ll dist[MAXN];
bool visited[MAXN];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for (int i = 0; i < m; i++)
{
cin >> a >> b >> w;
G[a].emplace_back(b, w);
}
for (int i = 0; i < MAXN; i++)
dist[i] = 8.7e17;
dist[1] = 0;
pq.emplace(0, 1);
while (!pq.empty())
{
auto tmp = pq.top();
pq.pop();
if (visited[tmp.second])
continue;
visited[tmp.second] = true;
for (auto [x, v] : G[tmp.second])
{
if (dist[x] > dist[tmp.second] + v)
{
dist[x] = dist[tmp.second] + v;
pq.emplace(dist[x], x);
}
}
}
for (int i = 1; i <= n; i++)
cout << dist[i] << " ";
cout << "\n";
return 0;
}