Pagini recente » Cod sursa (job #1915760) | Cod sursa (job #1284026) | Cod sursa (job #1450729) | Cod sursa (job #1701641) | Cod sursa (job #1754892)
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
int n, m, A[200002], c[200002];
long long cost_total;
struct muchie
{
int x,y,cost;
} G[400001],x;
void initializare()
{
ifstream f("apm.in");
f >> n >> m;
for(int i=1; i<=m; i++)
{
f >> G[i].x >> G[i].y >> G[i].cost;
}
for(int i=1; i<=n; i++)
c[i]=i;
f.close();
}
int compar(muchie a, muchie b)
{
if(a.cost<b.cost)
return 1;
else
return 0;
}
void afisare()
{
ofstream g("apm.out");
int i;
g<<cost_total<<"\n";
g<<n-1<<"\n";
for(i=1; i<n; i++)
g<<G[A[i]].x<<" "<<G[A[i]].y<<"\n";
g.close();
}
int main()
{
int i, j, minn, maxx, NrMSel=0;
initializare();
sort(G+1,G+m+1,compar);
for(i=1; NrMSel<n-1; i++)
{
if(c[G[i].x]!=c[G[i].y])
{
A[++NrMSel]=i;
cost_total=cost_total+G[A[NrMSel]].cost;
if(c[G[i].x]<c[G[i].y])
{
minn=c[G[i].x];
maxx=c[G[i].y];
}
else
{
minn=c[G[i].y];
maxx=c[G[i].x];
}
for(j=1; j<=n; j++)
if(c[j]==maxx) c[j]=minn;
}
}
afisare();
return 0;
}