Pagini recente » Cod sursa (job #1713103) | Cod sursa (job #2741055) | Cod sursa (job #1711111) | Cod sursa (job #2050986) | Cod sursa (job #1881011)
#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,ai;
ifstream f("apm.in");
ofstream g("apm.out");
int test (muchie a,muchie d)
{
return a.c<d.c;
}
int getroot(int x) {
if(t[x] != x) {
t[x] = getroot(t[x]);
}
return t[x];
}
void reunion(int x, int y) {
t[getroot(x)] = getroot(y);
}
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;
for(int i=1;i<=m and nr<n;++i)
{
if (getroot(v[i].x)!= getroot(v[i].y))
{
nr++;
sum+=v[i].c;
sol[nr]=i;int z=t[v[i].y]; ai=t[v[i].x];
reunion(v[i].x , v[i].y);
}
}
g<<sum<<'\n'<<n-1<<'\n';
for(int i=1;i<=nr;++i)
g<<v[sol[i]].x<<" "<<v[sol[i]].y<<endl;
g.close();
return 0;
}