Pagini recente » Cod sursa (job #2852754) | Cod sursa (job #1806505) | Rating Anca Radu (anca1911) | Cod sursa (job #2811806) | Cod sursa (job #1650925)
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
ifstream f("apm.in");
ofstream g("apm.out");
int viz[101];
int i, j, n, m, a, b, c;
struct muchie {int nod1; int nod2; int cost;};//muchie nod1 - nod2
muchie arb[400002];
bool compare(muchie lhs, muchie rhs) {
return (lhs.cost < rhs.cost);
}
int main()
{
f>>n>>m;
for (i=0;i<m;i++)
f>>arb[i].nod1>>arb[i].nod2>>arb[i].cost;
sort (arb, arb+m, compare);
int nr=1, ct;
viz[arb[i].nod1] = viz[arb[i].nod2] = 1;
ct = arb[1].cost;//ct = cost total
while (nr!=n-1) {
i=1;
while (viz[arb[i].nod1] + viz[arb[i].nod2] != 1)
i++;
viz[arb[i].nod1] = 1;
viz[arb[i].nod2] = 1;
nr++;
ct = ct + arb[i].cost;
}
g<<ct;
return 0;
}