prims c++
C++
#include <iostream>
#include <vector>
#include <queue>
#include <functional>
#include <utility>
using namespace std;
const int MAX = 1e4 + 5;
typedef pair<long long, int> PII;
bool marked[MAX];
vector <PII> adj[MAX];
long long prim(int x)
{
priority_queue<PII, vector<PII>, greater<PII> > Q;
int y;
long long minimumCost = 0;
PII p;
Q.push(make_pair(0, x));
while(!Q.empty())
{
// Select the edge with minimum weight
p = Q.top();
Q.pop();
x = p.second;
// Checking for cycle
if(marked[x] == true)
continue;
minimumCost += p.first;
marked[x] = true;
for(int i = 0;i < adj[x].size();++i)
{
y = adj[x][i].second;
if(marked[y] == false)
Q.push(adj[x][i]);
}
}
return minimumCost;
}
int main()
{
int nodes, edges, x, y;
long long weight, minimumCost;
cin >> nodes >> edges;
for(int i = 0;i < edges;++i)
{
cin >> x >> y >> weight;
adj[x].push_back(make_pair(weight, y));
adj[y].push_back(make_pair(weight, x));
}
// Selecting 1 as the starting node
minimumCost = prim(1);
cout << minimumCost << endl;
return 0;
}
Also in C++:
- Title
- count number of zeros in array in O(logN)
- Category
- C++
- Title
- strchr function in c++
- Category
- C++
- Title
- c++ std::copy to cout
- Category
- C++
- Title
- maximum subarray sum equal with K in c++
- Category
- C++
- Title
- what is time complexity of insertion sort
- Category
- C++
- Title
- cloud hosting
- Category
- C++
- Title
- how print fload wiht 3 decimal in c++
- Category
- C++
- Title
- how to have a queue as a parameter in c++
- Category
- C++
- Title
- substr c++
- Category
- C++
- Title
- switch statement c++
- Category
- C++
- Title
- c++ char print width
- Category
- C++
- Title
- pionter in c++
- Category
- C++
- Title
- min coin change problem dp
- Category
- C++
- Title
- map insert c++
- Category
- C++
- Title
- git branch in my bash prompt
- Category
- C++
- Title
- power c++
- Category
- C++
- Title
- c++ code to print hello world
- Category
- C++
- Title
- inheritance protected in c++
- Category
- C++
- Title
- c++ not greater than
- Category
- C++
- Title
- c++ function return pointer to itself
- Category
- C++
- Title
- insertion c++
- Category
- C++
- Title
- c++ class inheritance
- Category
- C++
- Title
- kruskal's algorithm c++ hackerearth
- Category
- C++
- Title
- apple and orange hackerrank solution in c++
- Category
- C++
- Title
- what is difference between ciel and floor
- Category
- C++
- Title
- how to convert qt string to string
- Category
- C++
- Title
- namespace file linking c++
- Category
- C++
- Title
- best fit algorithm
- Category
- C++
- Title
- map arduino
- Category
- C++
- Title
- ceil c++;
- Category
- C++
- Title
- array syntax in c++
- Category
- C++
- Title
- c++ get length of array
- Category
- C++
- Title
- c++ show time elapsed
- Category
- C++
- Title
- what is meaning of 64 bit integer in c++
- Category
- C++
- Title
- calculate factorial
- Category
- C++
- Title
- c++ string to int
- Category
- C++
- Title
- new in c++
- Category
- C++
- Title
- c++ get last element in vector
- Category
- C++
- Title
- c++ compiler for sublime text
- Category
- C++
- Title
- insert elements in array in c++11
- Category
- C++
- Title
- make an x using asterisk c++
- Category
- C++
- Title
- initialize map c++
- Category
- C++
- Title
- c++ how to make a negative float positive
- Category
- C++
- Title
- unordered_set in c++ and ordered set diff
- Category
- C++
- Title
- cube mapping sdl
- Category
- C++
- Title
- c++ how to skip the last element of vector
- Category
- C++
- Title
- convert int to binary string c++
- Category
- C++
- Title
- Html tab
- Category
- C++
- Title
- c++ reverse vector
- Category
- C++
- Title
- create a 2d array c++
- Category
- C++
- Title
- how to check type in c++
- Category
- C++
- Title
- SFML window
- Category
- C++
- Title
- vector in c++ class
- Category
- C++
- Title
- c++ movment
- Category
- C++
- Title
- variabili in c++
- Category
- C++
- Title
- find in string c++
- Category
- C++
- Title
- passing array to function c++ pointer
- Category
- C++
- Title
- differentialble programming
- Category
- C++
- Title
- arrays in C++
- Category
- C++
- Title
- random number generator c++
- Category
- C++
- Title
- Check if a Number is Odd or Even using Bitwise Operators
- Category
- C++
- Title
- shuffle vector c++
- Category
- C++
- Title
- namespaces c++
- Category
- C++
- Title
- c++ clamp
- Category
- C++
- Title
- cpp how to create an object of template class
- Category
- C++
- Title
- subarray sum in c++
- Category
- C++
- Title
- sieve of eratosthenes c++
- Category
- C++
- Title
- lisy stl C++
- Category
- C++
- Title
- msdn parse command line
- Category
- C++
- Title
- cpp nan value
- Category
- C++
- Title
- how to measure program run time in c++
- Category
- C++
- Title
- COnvert string to char * C++
- Category
- C++
- Title
- stl sort in c++
- Category
- C++
- Title
- sort a string alphabetically c++
- Category
- C++
- Title
- UPARAM(ref)
- Category
- C++
- Title
- c++ calculator program using switch case
- Category
- C++
- Title
- do while loop c++
- Category
- C++
- Title
- % operator in c++
- Category
- C++
- Title
- c++ program how to let the user choose different game modes
- Category
- C++
- Title
- binary search stl in c++
- Category
- C++
- Title
- cpp create lambda with recursion
- Category
- C++
- Title
- primitive and non primitive data types in c++
- Category
- C++
- Title
- coping 2d vector in cpp
- Category
- C++
- Title
- new keyword in cpp
- Category
- C++
- Title
- is x prime?
- Category
- C++
- Title
- how to output to console c++
- Category
- C++
- Title
- pass by reference c++
- Category
- C++
- Title
- c++ string to vector int
- Category
- C++
- Title
- c++ switch
- Category
- C++
- Title
- if vector contains value c++
- Category
- C++
- Title
- never gonna give you up lyrics
- Category
- C++
- Title
- c++ empty stream
- Category
- C++
- Title
- char vector to string c++
- Category
- C++
- Title
- switch c++
- Category
- C++
- Title
- matrix multiplication c++ eigen
- Category
- C++
- Title
- placement new c++
- Category
- C++
- Title
- how to use wasd c++
- Category
- C++
- Title
- appending a double to a string c++
- Category
- C++
- Title
- reverse a linked list using recursion
- Category
- C++
- Title
- basic ex of maps in c++
- Category
- C++