Pagini recente » Borderou de evaluare (job #2850972) | Cod sursa (job #2861303)
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
#include <bitset>
using namespace std;
ifstream in("apm.in");
ofstream out("apm.out");
const int N = 2e5;
const int INF = 1e9;
int n, m, cost;
bitset<N + 5> apm;
vector<int> dist(N + 5, INF);
vector<pair<int, int>> adj[N + 5], ans, father(N + 5);
priority_queue<pair<int, int>> pq;
int main() {
in >> n >> m;
for (int i = 1; i <= m; i++) {
int x, y, c;
in >> x >> y >> c;
adj[x].emplace_back(y, c);
adj[y].emplace_back(x, c);
}
dist[1] = 0;
pq.emplace(0, 1);
while (!pq.empty()) {
int node = pq.top().second;
pq.pop();
if (apm[node])
continue;
apm[node] = true;
if(father[node].first != 0) {
cost += father[node].second;
ans.emplace_back(father[node].first, node);
}
for (auto it: adj[node]) {
if (!apm[it.first] && dist[it.first] > it.second) {
dist[it.first] = it.second;
father[it.first] = make_pair(node, it.second);
pq.emplace(-dist[it.first], it.first);
}
}
}
out << cost << '\n' << ans.size() << '\n';
for (auto it: ans)
out << it.first << ' ' << it.second << '\n';
return 0;
}