Pagini recente » Cod sursa (job #1128140) | Cod sursa (job #1788991) | Cod sursa (job #2189854) | Cod sursa (job #2100441) | Cod sursa (job #2214807)
#include <bits/stdc++.h>
#define NMAX 200005
#define MMAX 400005
using namespace std;
ifstream fin ("apm.in");
ofstream fout ("apm.out");
int n, m, minimum_Cost;
int node_x[NMAX], node_y[NMAX], cost[NMAX], index[NMAX], father[NMAX];
vector <int> nodes;
class cmp
{
public:
const bool operator () (int a, int b )
{
return cost[a] < cost[b];
}
};
int parent( int node )
{
if ( node == father[node] )
return node;
father[node] = parent(father[node]);
return father[node];
}
void unite ( int first_Node, int second_Node )
{
father[parent(first_Node)] = parent(second_Node);
}
int main()
{
fin>>n>>m;
for ( int i = 1; i <= n; ++i )
father[i] = i;
for ( int i = 1; i <= m; ++i )
{
fin>>node_x[i]>>node_y[i]>>cost[i];
index[i] = i;
}
sort(index+1, index+m+1, cmp());
for ( int i = 1; i <= m; ++i )
{
if ( parent(node_x[index[i]]) != parent(node_y[index[i]]) )
{
minimum_Cost += cost[index[i]];
nodes.push_back(index[i]);
unite(node_x[index[i]], node_y[index[i]]);
}
}
fout<<minimum_Cost<<'\n'<<nodes.size()<<'\n';
for ( auto i:nodes )
fout<<node_x[i]<<" "<<node_y[i]<<'\n';
}