Pagini recente » Cod sursa (job #2622162) | Cod sursa (job #913127) | Cod sursa (job #1886894) | Cod sursa (job #1516404) | Cod sursa (job #3238662)
#include <bits/stdc++.h>
using namespace std;
const int MAX = 200005;
bitset<MAX> visited;
vector<int> cost;
vector<pair<int,int>> graph[MAX];
priority_queue< pair<int, pair<int,int>>, vector<pair<int, pair<int,int>>>, greater<pair<int, pair<int,int>>> > q;
// cost, nod nou, nod vechi
int main()
{
ifstream cin("apm.in");
ofstream cout("apm.out");
int n, m;
cin >> n >> m;
cost.assign(n+1, 1e9);
for (int i = 1; i <= m; ++i)
{
int x, y, cost;
cin >> x >> y >> cost;
graph[x].push_back({y, cost});
graph[y].push_back({x, cost});
}
for (auto x : graph[1])
{
cost[x.first] = x.second;
q.push({x.second, {x.first, 1}});
}
int sum = 0;
vector<pair<int,int>> path;
visited[1] = 1;
for (int i = 1; i < n; ++i)
{
while (visited[q.top().second.first]) // iau nodul nevizitat
q.pop();
int node = q.top().second.first;
visited[node] = 1;
path.push_back({q.top().second.second, node});
sum += q.top().first;
for (auto x : graph[node])
{
cost[x.first] = min(cost[x.first], x.second);
q.push({x.second, {x.first, node}});
}
}
cout << sum << "\n" << n-1 << "\n";
for (auto x : path)
cout << x.first << " " << x.second << "\n";
return 0;
}