Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <set>
- #include <vector>
- #include <deque>
- #include <map>
- #include <cmath>
- #define se second
- #define fi first
- #define mp make_pair
- #define pb push_back
- typedef long long ll;
- typedef long double ld;
- using namespace std;
- ll inf = (ll)1e9 + 5;
- int main() {
- int n, m;
- cin >> n >> m;
- vector<vector<pair<int, int>>> g(n);
- while (m--) {
- int x, y, w;
- cin >> x >> y >> w;
- x--, y--;
- g[x].pb({y, w});
- g[y].pb({x, w});
- }
- vector<int> w(n, inf);
- w[0] = 0;
- set<pair<int, int>> st;
- st.insert({0, 0});
- while (!st.empty()) {
- pair<int, int> cur = *st.begin();
- int h = cur.fi;
- for (auto e : g[h]) {
- if (w[e.se] > w[h] + e.fi) {
- w[e.se] = w[h] + e.fi;
- st.insert({w[e.se], e.se});
- }
- }
- }
- for (auto e : w) {
- if (e == inf) cout << "-1" << ' ';
- else cout << e << ' ';
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment