Pagini recente » Cod sursa (job #2198850) | Cod sursa (job #391376) | Cod sursa (job #892480) | Cod sursa (job #966396) | Cod sursa (job #1880984)
#include<fstream>
#include<algorithm>
using namespace std;
struct muchie
{
int x,y,c;
};
muchie v[400001];
int cost,t[400001],sol[400001],n,nr,m,sum;
ifstream f("apm.in");
ofstream g("apm.out");
int test (muchie a,muchie d)
{
return a.c<d.c;
}
int main()
{
f>>n>>m;
for(int i=1;i<=m;++i)
f>>v[i].x>>v[i].y>>v[i].c;
sort(v+1,v+m+1,test);
for(int i=1;i<=n;++i)
t[i]=i;
nr=1;
sol[1]=1;sum=v[1].c;
t[v[1].y]=t[v[1].x];
for(int i=2;i<=m and nr<n-1;++i)
{
if (t[v[i].x]==t[v[i].y]) continue;
nr++;
sum+=v[i].c;
sol[nr]=i;int z=t[v[i].y];
for(int j=1;j<=n;++j)
if(t[j]==z) t[j]=t[v[i].x];
}
g<<sum<< '\n';
g << nr << '\n';
for(int i=1;i<=nr;++i)
g<<v[sol[i]].x<<" "<<v[sol[i]].y<<endl;
g.close();
return 0;
}