Pagini recente » Cod sursa (job #386109) | Cod sursa (job #246425) | Cod sursa (job #1929697) | Cod sursa (job #522257) | Cod sursa (job #1166501)
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
ifstream f("apm.in");
ofstream g("apm.out");
int n,m,x[400005],y[400005],c[400005],ind[400005],gr[200005],dist,i;
vector <int> much;
bool cmp(int xx,int yy)
{
return(c[xx]<c[yy]);
}
int group(int xx)
{
if (gr[xx]==xx) return xx;
gr[xx]=group(gr[xx]);
return gr[xx];
}
void unite (int xx, int yy)
{
gr[group(xx)]=group(yy);
}
int main ()
{
f>>n>>m;
for (i=1;i<=m;i++)
{
f>>x[i]>>y[i]>>c[i];
ind[i]=i;
}
sort(ind+1,ind+m+1,cmp);
for (i=1;i<=n;i++)
gr[i]=i;
for (i=1;i<=m;i++)
if (group(x[ind[i]])!=group(y[ind[i]]))
{
dist+=c[ind[i]];
unite(x[ind[i]],y[ind[i]]);
much.push_back(ind[i]);
}
g<<dist<<"\n"<<much.size()<<"\n";
for (i=0;i<much.size();i++)
g<<x[much[i]]<<" "<<y[much[i]]<<"\n";
}