Cod sursa(job #2633569)

Utilizator Alex_tz307Lorintz Alexandru Alex_tz307 Data 7 iulie 2020 19:38:57
Problema Arbore partial de cost minim Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.2 kb
#include <bits/stdc++.h>
#define NMAX 200002

using namespace std;

ifstream fin ("apm.in");
ofstream fout ("apm.out");

typedef pair < int, int > Pair;
int N, M, ans;
vector < Pair > G[NMAX];
list < Pair > sol;
priority_queue < Pair, vector < Pair > , greater < Pair > > Heap;
vector < int > key (NMAX, INT_MAX), parent (NMAX);
vector < bool > viz (NMAX);

void Read () {
  fin >> N >> M;
  while (M --) {
    int u, v, wt;
    fin >> u >> v >> wt;
    G[u].push_back ({v, wt});
    G[v].push_back ({u, wt});
  }
}

void Prim ()  {
  Heap.push ({0, 1});
  key[1] = 0;
  while (!Heap.empty())  {
    int u = Heap.top().second;
    Heap.pop();
    viz[u] = true;
    for (auto it : G[u])  {
      int v = it.first, weight = it.second;
      if (viz[v] == false && key[v] > weight)  {
        key[v] = weight;
        Heap.push ({key[v], v});
        parent[v] = u;
      }
    }
  }
  for (int i = 2; i <= N; i ++) {
    sol.push_back ({parent[i], i});
    ans += key[i];
  }
}

void Solve () {
  Read();
  Prim();
  fout << ans << '\n' << N - 1 << '\n';
  for (auto it : sol)
    fout << it.first << ' ' << it.second << '\n';
}

int main() {
  Solve();
  return 0;
}