Pagini recente » Istoria paginii runda/eusebiu_oji_2009_cls9/clasament | Cod sursa (job #1298265) | Cod sursa (job #1860144) | Cod sursa (job #963419) | Cod sursa (job #1978097)
#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;
}
};
vector<muchie> v;
bool sortcomp(muchie i,muchie j)
{
return i.cost<j.cost;
}
void citire()
{
muchie nou;
int i,j,y,c;
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);
}
}
int *hi=new int [n+1];
int find(int *o,int n)
{
int k=n,h=0;
while(o[k]!=-1)
{
if(o[k]!=-1) k=o[k];
h++;
}
hi[k]=h;
return k;
}
int main()
{
int i;
int cs = 0,k=0;
citire();
int *g=new int[n+1];
// int *hi=new int [n+1];
vector<muchie> x;
for(i=0;i<=n;i++) g[i]=-1,hi[i]=0;
sort(v.begin(),v.end(),sortcomp);
for(muchie i:v)
{
if(find(g,i.out)!=find(g,i.in))
{
cs=cs+i.cost;
k++;
if(hi[i.out]<=hi[i.in])
g[find(g,i.out)]=i.in;
else g[find(g,i.in)]=i.out;
x.push_back(i);
}
}
g1<<cs<<endl<<k<<endl;
for(muchie i:x)
g1<<i.in<<" "<<i.out<<endl;
return 0;
}