Pagini recente » Cod sursa (job #3030005) | Cod sursa (job #2217478) | Cod sursa (job #2171985) | Cod sursa (job #1592494) | Cod sursa (job #1978179)
#include <iostream>
#include <algorithm>
#include <fstream>
#include <vector>
#include<set>
using namespace std;
ifstream f("apm.in");
ofstream g1("apm.out");
int m,n;
struct muchie
{
int in,out,cost;
bool operator <(const muchie& pt) const
{
return cost<=pt.cost;
}
}v[400001];
//vector<muchie> v;
//int *g=new int[n+1]();
//int *hi=new int [n+1]();
int g[200001],hi[200001];
bool sortcomp(muchie i,muchie j)
{
return i.cost<j.cost;
}
void citire()
{
muchie nou;
int i,j,y,c,l=0;
f>>n;
f>>m;
for(i=0; i<m; i++)
{
f>>j>>y>>c;
nou.in=j;
nou.out=y;
nou.cost=c;
//v.push_back(nou);
v[l++]=nou;
}
}
int find(int n)
{
int k=n,ok,prec;
while(g[k])
k=g[k];
ok=k;
k=n;
while(g[k]){
prec=k;
k=g[k];
g[prec]=ok;
}
return ok;
}
void R(int p,int q)
{
if(hi[p]>hi[q]) g[q]=p;
else
if(hi[p]<hi[q])
g[p]=q;
else
{
g[q]=p;
hi[p]++;
}
}
int main()
{
int cs = 0,nr=0,i;
citire();
//vector<muchie> x;
muchie x[200001];
sort(v,v+m,sortcomp);
for(i=0;i<m;i++)
{
int p=find(v[i].in);
int q=find(v[i].out);
if(p!=q)
{
cs=cs+v[i].cost;
R(p,q);
//x.push_back(i);
x[nr]=v[i];
nr++;
// if(nr==n-2) break;
}
}
g1<<cs<<endl<<nr<<endl;
for(i=0;i<m-1;i++)
g1<<x[i].in<<" "<<x[i].out<<endl;
return 0;
}