Cod sursa(job #2071618)

Utilizator DrumeaVDrumea Vasile DrumeaV Data 20 noiembrie 2017 20:24:18
Problema Arbore partial de cost minim Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 3.34 kb
/*#include <iostream>
#include <fstream>
#include <vector>
#include <utility>
#include <algorithm>

using namespace std;

ifstream fin("text.in");

struct Node{
    int first;
    int second;
    int weight;
};

void init(int &nodes, vector< int > &Parent){
    for (int i = 1; i <= nodes; i++)
        Parent[i] = i;
}

int Root(int n, vector< int > &Parent){
    if (Parent[n] == n)
       return n;

    Parent[n] = Root(Parent[n], Parent);
    return Parent[n];
}

void Union(int x, int y, vector< int > &Parent){
    Parent[Root(x, Parent)] = Root(y, Parent);
}

bool cmp(Node a, Node b){
    return a.weight <= b.weight;
}

int main(){
    clock_t begin = clock();
    int nodes, edges;
    fin >> nodes >> edges;
    vector< Node > Graph;
    vector< Node > MST;
    vector< int > Parent(nodes + 1);
    init(nodes, Parent);

    for (int i = 0; i < edges; i++){
        int x, y, w;
        Node aux;
        fin >> x >> y >> w;
        aux.first = x;
        aux.second = y;
        aux.weight = w;
        Graph.push_back(aux);
    }

    sort(Graph.begin(), Graph.end(), cmp);
    int ans = 0;
    for (int i = 0; i < edges; i++){
        if (Root(Graph[i].first, Parent) != Root(Graph[i].second, Parent)){
            ans += Graph[i].weight;
            Union(Graph[i].first, Graph[i].second, Parent);
            MST.push_back(Graph[i]);
        }
    }

    /**fout << "The minimal cost is :\n";
    fout << ans << endl;
    //fout << "And is composed of the following edges :\n";
    //for (int i = 0; i < MST.size(); i++)
        //fout << MST[i].first << " " << MST[i].second << " " << MST[i].weight << endl;

    clock_t end = clock();
    double sec = double(end - begin) / CLOCKS_PER_SEC;
    cout << "Time = " << sec << "\n";
    return 0;
}
*/
#include <iostream>
#include <fstream>
#include <vector>
#include <utility>
#include <algorithm>

using namespace std;

ifstream fin("apm.in");
ofstream fout("apm.out");

struct Node{
    int first;
    int second;
    int weight;
};

void init(int &nodes, vector< int > &Parent){
    for (int i = 1; i < nodes; i++)
        Parent[i] = i;
}

int Root(int n, vector< int > &Parent){
    if (Parent[n] == n)
       return n;

    Parent[n] = Root(Parent[n], Parent);
    return Parent[n];
}

void Union(int x, int y, vector< int > &Parent){
    Parent[Root(x, Parent)] = Root(y, Parent);
}

bool cmp(Node a, Node b){
    return a.weight <= b.weight;
}

int main(){
    int nodes, edges;
    fin >> nodes >> edges;
    vector< Node > Graph;
    vector< Node > MST;
    vector< int > Parent(nodes);
    init(nodes, Parent);

    for (int i = 0; i < edges; i++){
        int x, y, w;
        Node aux;
        fin >> x >> y >> w;
        aux.first = x;
        aux.second = y;
        aux.weight = w;
        Graph.push_back(aux);
    }

    sort(Graph.begin(), Graph.end(), cmp);
    int ans = 0;
    for (int i = 0; i < edges; i++){
        if (Root(Graph[i].first, Parent) !=
        Root(Graph[i].second, Parent)){
            ans += Graph[i].weight;
            Union(Graph[i].first, Graph[i].second,
            Parent);
            MST.push_back(Graph[i]);
        }
    }

    cout << "The minimal cost is :\n";
    cout << ans << endl;
    cout << "And is composed of the following edges :\n";
    for (int i = 0; i < MST.size(); i++)
        cout << MST[i].first << " " << MST[i].second
        << " " << MST[i].weight << endl;
    return 0;
}