Pagini recente » Cod sursa (job #1922926) | Cod sursa (job #2009082) | Cod sursa (job #2170051) | Cod sursa (job #3137689) | Cod sursa (job #2027739)
#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
#define NMAX 200005
#define MAX 0x3f3f3f
using namespace std;
int n, m, viz[NMAX], dist[NMAX], tata[NMAX];
priority_queue <pair <int, int> > Q;
vector <pair <int, int> > g[NMAX];
vector <pair <int, int> >::iterator it;
void read()
{
scanf("%d%d", &n, &m);
for(int i=1; i<=m; ++i)
{
int x, y, c;
scanf("%d%d%d", &x, &y, &c);
g[x].push_back(make_pair(y, c));
g[y].push_back(make_pair(x, c));
}
dist[1] = 0;
tata[1] = 1;
viz[1] = 1;
for(int i=2; i<=n; ++i)
{
tata[i] = i;
dist[i] = MAX;
}
}
void solvePrimAlgorithm()
{
Q.push(make_pair(0, 1));
while(!Q.empty())
{
int cost = -1 * Q.top().first;
int nodCurent = Q.top().second;
Q.pop();
viz[nodCurent] = 1;
for(it = g[nodCurent].begin(); it != g[nodCurent].end(); ++it)
if(viz[it->first]==0 && dist[it->first] > it->second)
{
dist[it->first] = it->second;
tata[it->first] = nodCurent;
Q.push(make_pair(-1*it->second, it->first));
}
}
}
void afisare()
{
int distTotatla = 0;
for(int i=2; i<=n; ++i)
distTotatla += dist[i];
printf("%d\n", distTotatla);
printf("%d\n", n-1);
for(int i=2; i<=n; ++i)
printf("%d %d\n", tata[i], i);
}
int main()
{
freopen("apm.in", "r", stdin);
freopen("apm.out", "w", stdout);
read();
solvePrimAlgorithm();
afisare();
return 0;
}