Pagini recente » Cod sursa (job #159878) | Cod sursa (job #2667390) | Cod sursa (job #3168646) | Cod sursa (job #2741028) | Cod sursa (job #2328034)
#include <bits/stdc++.h>
using namespace std;
ifstream f("apm.in");
ofstream g("apm.out");
struct muchie{
int x,y;
double c;
};
bool cmp(muchie a, muchie b)
{
return a.c<b.c;
}
queue <pair <int,int> > q;
muchie v[400001];
int gr[200001];
int n,m;
int main()
{
f>>n>>m;
int i,j;
for(i=1;i<=n;i++)
gr[i]=i;
for(i=1;i<=m;i++)
{
f>>v[i].x>>v[i].y>>v[i].c;
}
sort(v+1,v+m+1,cmp);
double s=0;
int k=0,kk;
for(i=1;i<=m,k<n-1;i++)
{
if(gr[v[i].x]!=gr[v[i].y])
{
k++;
s+=v[i].c;
q.push(make_pair(v[i].x,v[i].y));
kk==gr[v[i].y];
for(j=1;j<=n;j++)
if(gr[j]==kk)
gr[j]=gr[v[i].x];
}
}
g<<s<<"\n"<<n-1<<"\n";
while(q.empty()==false)
{
g<<q.front().first<<" "<<q.front().second<<"\n";
q.pop();
}
return 0;
}