Pagini recente » Cod sursa (job #759463) | Cod sursa (job #2559187) | Cod sursa (job #665567) | Cod sursa (job #54017) | Cod sursa (job #1881017)
#include<fstream>
#include<algorithm>
#include<vector>
using namespace std;
struct muchie
{
int x,y,c;
};
//muchie v[400000];
vector <muchie> v;
int cost,t[400000],sol[400000],n,nr,m,sum;
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;
muchie w;
for(int i=1;i<=m;++i)
{f>>w.x>>w.y>>w.c;
v.push_back(w);}
sort(v.begin(),v.end(),test);
for(int i=1;i<=n;++i)
t[i]=i;
nr=1;
sol[1]=0;sum=v[0].c;
t[v[0].y]=t[v[0].x];
reunion(v[0].x, v[0].y);
for(int i=1;i<=m and nr<n-2;++i)
{
if (getroot(v[i].x)==getroot(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];
reunion(v[i].x,v[i].y);
}
g<<sum<<endl<<n-1<<endl;
for(int i=1;i<=nr;++i)
g<<v[sol[i]].x<<" "<<v[sol[i]].y<<endl;
g.close();
return 0;
}