Pagini recente » Cod sursa (job #236317) | Cod sursa (job #2222351) | Cod sursa (job #1136838) | Cod sursa (job #2433805) | Cod sursa (job #2556549)
///graf neorientat = determ un arbore partial de cost minim
#include <queue>
#include <iostream>
#include <fstream>
#include<algorithm>
using namespace std;
ifstream f("apm.in");
ofstream g("apm.out");
struct muchie
{
int x, y;
double c;
bool operator<(const muchie &next)
{
if(c <= next.c)
return true;
return false;
}
} v[200];
int n, m, t[200];
void citire()
{
f>>n>>m;
for(int i=1; i<=m; i++)
f>>v[i].x>>v[i].y>>v[i].c;
}
/*
int Radacina(int x) {
int rad = x;
int y = x;
while(t[x] != x) {
x = t[x];
}
rad = t;
while(c[y] != y) {
int aux = c[y];
c[y] = rad;
y = aux;
}
}
*/
void unire(int x, int y) {
for(int i=1;i<=n;i++)
if(t[i] == y)
t[i] = x;
}
void sortare()
{
sort(v+1,v+1+m);
}
void init()
{
for(int i=1;i<=n;i++)
t[i] = i;
}
void apm()
{
double ct = 0;
vector<pair<int,int>>V;
int nr = 0, ok = 0;
for(int i=1; i<=m && ok == 0;i++)
if(t[v[i].x] != t[v[i].y])
{
ct += v[i].c;
unire(t[v[i].x], t[v[i].y]);
nr++;
V.push_back({v[i].x,v[i].y});
if(nr == n-1)
ok = 1;
}
g << ct<<"\n" <<V.size() << "\n";
for(auto y : V) {
g<<y.first<< " "<<y.second << "\n";
}
}
int main()
{
citire();
init();
sortare();
apm();
return 0;
}