Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define int long long
- #define pii pair<long long,long long>
- using namespace std;
- const int N = 1e5 + 1;
- vector<pii> gr[N];
- int dist[N];
- int32_t main() {
- int n, m;
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- freopen("pathbgep.in", "r", stdin);
- freopen("pathbgep.out", "w", stdout);
- cin >> n >> m;
- for (int i = 0 ; i < m; ++ i) {
- int a, b, c;
- cin >> a >> b >> c;
- -- a; -- b;
- gr[a].push_back({b, c});
- gr[b].push_back({a, c});
- }
- for (int i = 0; i < n; ++ i) {
- dist[i] = LLONG_MAX;
- }
- priority_queue<pii, vector<pii>, greater<pii> > pq;
- dist[0] = 0;
- pq.push({0, 0});
- while (!pq.empty()) {
- pii cur = pq.top();
- pq.pop();
- int dst = cur.first, v = cur.second;
- if (dst > dist[v]) {
- continue;
- }
- for (pii i : gr[v]) {
- int dst_ = dst + i.second;
- if (dst_ < dist[i.first]) {
- dist[i.first] = dst_;
- pq.push({dst_, i.first});
- }
- }
- }
- for (int i = 0; i < n; ++ i) {
- cout << dist[i] << ' ';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement