Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include"./template/headers.hpp"
- //#define MULTI_TASKCASE
- using namespace abb;
- using namespace output;
- using namespace rit;
- using namespace wit;
- inline void init() {
- }
- struct edge {
- int b, w;
- edge(int x, int y): b(x), w(y) {}
- };
- bool operator<(edge a, edge b) {return a.w > b.w;}
- inline void solve() {
- int n, m;
- while (cin >> n >> m) {
- V<V<edge>> v(n);
- for (int i = 0; i != m; i++) {
- static int a, b, w;
- cin >> a >> b >> w;
- v[a].emplace_back(b, w);
- v[b].emplace_back(a, w);
- }
- V<bool> vis(n, false);
- V<ll> d(n, 1e15);
- PQ<edge> pq; pq.emplace(0, d[0] = 0);
- for (int i = 0; i < n; i++) {
- static int a; a = ~0u;
- while (pq.size() && vis[a = pq.top().b]) pq.pop();
- if (!~a) break;
- vis[a] = true;
- for (const auto& i : v[a])
- if (!vis[i.b] && i.w < d[i.b])
- pq.emplace(i.b, d[i.b] = i.w);
- }
- if (find(d.begin(), d.end(), 1e15) != d.end())
- cout << -1 << endl;
- else
- cout << accumulate(d.begin(), d.end(), 0LL) << endl;
- }
- }
- main() {
- ios::sync_with_stdio(false);
- init();
- int t = 1;
- #ifdef MULTI_TASKCASE
- cin >> t; cin.ignore();
- #endif
- while (t--) solve();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement