Pagini recente » Cod sursa (job #2824959) | Cod sursa (job #220773) | Cod sursa (job #1107678) | Cod sursa (job #767400) | Cod sursa (job #1978177)
#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;
citire();
vector<muchie> x;
sort(v,v+m,sortcomp);
for(muchie i:v)
{
int p=find(i.in);
int q=find(i.out);
if(p!=q)
{
cs=cs+i.cost;
R(p,q);
x.push_back(i);
nr++;
if(nr==n-1) break;
}
}
g1<<cs<<endl<<nr<<endl;
for(muchie i:x)
g1<<i.in<<" "<<i.out<<endl;
return 0;
}