From 3f5e08a48b6c62072e2a8bc00e7f33151e9f894d Mon Sep 17 00:00:00 2001 From: Shrey Gupta <75122294+Shreygupta2002@users.noreply.github.com> Date: Sat, 7 Oct 2023 13:43:06 +0530 Subject: [PATCH] Added Prims Algorithm --- c++/prims.cpp | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 c++/prims.cpp diff --git a/c++/prims.cpp b/c++/prims.cpp new file mode 100644 index 0000000..9a0e5e4 --- /dev/null +++ b/c++/prims.cpp @@ -0,0 +1,64 @@ +#include +using namespace std; +typedef long long int ll; +int INF = 1e9; +int main() +{ + ios::sync_with_stdio(0);cin.tie(0); + mt19937 rng((unsigned int) chrono::steady_clock::now().time_since_epoch().count()); + auto start=chrono::high_resolution_clock::now(); + + //code here + + int vertices, edges; + cin>>vertices>>edges; + vector> graph[vertices+1]; + for(int i=0;i>u>>v>>w; + graph[u].push_back({v,w}); + graph[v].push_back({u,w}); + } + int parent[vertices+1]; + int key[vertices+1]; + bool mst[vertices+1]; + for(int i=1;i<=vertices;i++) + { + key[i]=INF; + mst[i]=false; + parent[i]=-1; + } + + priority_queue,vector>, greater> > pq; + key[1]=0; + pq.push({0,1}); + for(int i=0;i(stop-start); + cerr<