Pagini recente » Cod sursa (job #193099) | Cod sursa (job #199470) | Cod sursa (job #568814) | Cod sursa (job #57768) | Cod sursa (job #2716840)
#include <fstream>
#include <math.h>
#include <set>
#include <queue>
#include <vector>
using namespace std;
ifstream fin ("apm.in");
ofstream fout ("apm.out");
int n, m, f[50001], pereche[50001], cost[50001];
vector <pair <int, int>> l[50001];
priority_queue <pair <int, int>> pq;
int main()
{
int x, y, c, i, nod1, nod2;
unsigned long long suma;
fin >> n >> m;
for(i=1; i<=m; i++)
{
fin >> x >> y >> c;
l[x].push_back({y, c});
l[y].push_back({x, c});
}
for (i = 1; i<= n; i++)
cost[i] = 1e9;
pq.push({0, 1});
while(pq.size() > 0)
{
nod1 = pq.top().second;
pq.pop();
f[nod1] = 1;
for (i = 0; i < l[nod1].size(); i++)
{
nod2 = l[nod1][i].first;
c = l[nod1][i].second;
if(f[nod2] == 0 && cost[nod2] > c)
{
cost[nod2] = c;
f[nod2] = 1;
pereche[nod2] = nod1;
pq.push({cost[nod2], nod2});
}
}
}
suma = 0;
for (i = 1; i<= n; i++)
if(cost[i] < 1e9)
suma +=cost[i];
fout << suma << '\n' << n-1 << '\n';
for (i = 1; i<= n; i++)
if(pereche[i] > 0)
fout << i << " " << pereche[i] << '\n';
}