Pagini recente » Cod sursa (job #2161793) | Cod sursa (job #765030) | Cod sursa (job #1821401) | Cod sursa (job #355432) | Cod sursa (job #3139118)
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
#define MAXN 200010
ifstream f("apm.in");
ofstream g("apm.out");
vector<pair<int, int>> G[MAXN];
priority_queue<pair<int, pair<int, int>>> PQ;
vector<pair<int, int>> muchii;
int n, m, nr, x, y, c, total;
int viz[MAXN];
int main()
{
f >> n >> m;
while (m--) {
f >> x >> y >> c;
G[x].push_back({y, c});
G[y].push_back({x, c});
}
for (auto it:G[1]){
PQ.push({-it.second, {it.first, 1}});
}
viz[1] = 1;
while (nr < n - 1) {
auto next = PQ.top();
PQ.pop();
auto nod = next.second.first;
auto prev = next.second.second;
if (viz[nod]) {
continue;
}
viz[nod] = 1;
total-=next.first;
++nr;
muchii.push_back({nod, prev});
for (auto it:G[nod]) {
if (!viz[it.first]) {
PQ.push({-it.second, {it.first, nod}});
}
}
}
g << total << '\n';
g << n - 1 << '\n';
for (auto it:muchii) {
g << it.first << " " << it.second << '\n';
}
return 0;
}