Pagini recente » Cod sursa (job #1404311) | Cod sursa (job #3185962) | Cod sursa (job #1246811) | Cod sursa (job #1141426) | Cod sursa (job #2575728)
#include <bits/stdc++.h>
using namespace std;
ifstream f("graf.in");
ofstream g("graf.out");
int n, m, a, b, c, aux, s;
struct muchie
{
int nod1;
int nod2;
int cost;
bool ales;
};
bool cmp(muchie a, muchie b){
return a.cost < b.cost;
}
int main()
{
muchie mch[400002];
f>>n>>m;
for(int i=1; i<=m; i++)
{
f>>a>>b>>c;
mch[i].nod1=a;
mch[i].nod2=b;
mch[i].cost=c;
}
sort(mch+1, mch+m+1, cmp);
int marcat[200002];
for(int i = 1; i <= n; i++) marcat[i] = 0;
marcat[1]=1;
s=0;
bool gasit;
for (int pas=1; pas<=n-1; pas++)
{
gasit=0;
for(int i=1; i<=m && !gasit; i++)
{
if ((marcat[mch[i].nod1] ==1 && marcat[mch[i].nod2]==0) ||
(marcat[mch[i].nod1] ==0 && marcat[mch[i].nod2]==1))
{
s=s+mch[i].cost;
marcat[mch[i].nod1]=1;
marcat[mch[i].nod2]=1;
gasit=1;
mch[i].ales = 1;
}
}
}
g<<s<<'\n';
g<<n-1<<'\n';
for(int i = 1; i <= m; i++)
if(mch[i].ales == 1)
g<<mch[i].nod1<<' '<<mch[i].nod2<<'\n';
return 0;
}