"kruskal's algorithm c++ hackerearth" Code Answer's

You're definitely familiar with the best coding language C++ that developers use to develop their projects and they get all their queries like "kruskal's algorithm c++ hackerearth" answered properly. Developers are finding an appropriate answer about kruskal's algorithm c++ hackerearth related to the C++ coding language. By visiting this online portal developers get answers concerning C++ codes question like kruskal's algorithm c++ hackerearth. Enter your desired code related query in the search bar and get every piece of information about C++ code related question on kruskal's algorithm c++ hackerearth. 

kruskal's algorithm c++ hackerearth

By Stormy SkylarkStormy Skylark on Jul 01, 2020
#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>
using namespace std;
const int MAX = 1e4 + 5;
int id[MAX], nodes, edges;
pair <long long, pair<int, int> > p[MAX];

void initialize()
{
    for(int i = 0;i < MAX;++i)
        id[i] = i;
}

int root(int x)
{
    while(id[x] != x)
    {
        id[x] = id[id[x]];
        x = id[x];
    }
    return x;
}

void union1(int x, int y)
{
    int p = root(x);
    int q = root(y);
    id[p] = id[q];
}

long long kruskal(pair<long long, pair<int, int> > p[])
{
    int x, y;
    long long cost, minimumCost = 0;
    for(int i = 0;i < edges;++i)
    {
        // Selecting edges one by one in increasing order from the beginning
        x = p[i].second.first;
        y = p[i].second.second;
        cost = p[i].first;
        // Check if the selected edge is creating a cycle or not
        if(root(x) != root(y))
        {
            minimumCost += cost;
            union1(x, y);
        }    
    }
    return minimumCost;
}

int main()
{
    int x, y;
    long long weight, cost, minimumCost;
    initialize();
    cin >> nodes >> edges;
    for(int i = 0;i < edges;++i)
    {
        cin >> x >> y >> weight;
        p[i] = make_pair(weight, make_pair(x, y));
    }
    // Sort the edges in the ascending order
    sort(p, p + edges);
    minimumCost = kruskal(p);
    cout << minimumCost << endl;
    return 0;
}

Source: www.hackerearth.com

Add Comment

0

All those coders who are working on the C++ based application and are stuck on kruskal's algorithm c++ hackerearth can get a collection of related answers to their query. Programmers need to enter their query on kruskal's algorithm c++ hackerearth related to C++ code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about kruskal's algorithm c++ hackerearth for the programmers working on C++ code while coding their module. Coders are also allowed to rectify already present answers of kruskal's algorithm c++ hackerearth while working on the C++ language code. Developers can add up suggestions if they deem fit any other answer relating to "kruskal's algorithm c++ hackerearth". Visit this developer's friendly online web community, CodeProZone, and get your queries like kruskal's algorithm c++ hackerearth resolved professionally and stay updated to the latest C++ updates. 

C++ answers related to "kruskal's algorithm c++ hackerearth"

View All C++ queries

C++ queries related to "kruskal's algorithm c++ hackerearth"

Browse Other Code Languages

CodeProZone