Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.4 kB
1
Indexable
Never
/******************************************************************************

Welcome to GDB Online.
  GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby, 
  C#, OCaml, VB, Perl, Swift, Prolog, Javascript, Pascal, COBOL, HTML, CSS, JS
  Code, Compile, Run and Debug online from anywhere in world.

*******************************************************************************/
#include <stdio.h>
#include <iostream>
#include <vector>
#include <set>
#include <queue>

using namespace std;

#define pii pair<int, int>

const int N = 410;

int n, m;
vector <pii> g[N];
bool first[N];
int a[N], b[N], w[N];
priority_queue <pii> q;

int main()
{
    cin >> n >> m;
    for (int i = 1; i <= m; i++) {
        cin >> a[i] >> b[i] >> w[i];
        g[a[i]].push_back({b[i], w[i]});
        g[b[i]].push_back({a[i], w[i]});
    }
    first[1] = true;
    for (pii p : g[1]) {
        int u = p.first;
        int w = p.second;
        q.push({-w, u});
    }
    long long ans = 0;
    while (q.size() > 0) {
        int v = q.top().second;
        int cost = -q.top().first;
        q.pop();
        if (first[v]) continue;
        first[v] = true;
        ans += cost;
        for (pii p : g[v]) {
            int w = p.second;
            int u = p.first;
            if (!first[u]) {
                q.push({-w, u});
            }
        }
    }
    cout << ans;
    return 0;
}