From ee2fb46155101cbaac471a7e7e4882482a6a3978 Mon Sep 17 00:00:00 2001 From: Ritik Nandwal Date: Wed, 21 Jul 2021 19:47:59 +0530 Subject: [PATCH] Add implementation for finding minimum cost to make path b/w src to dest --- graph/minEdgetomakepath.cpp | 43 +++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 graph/minEdgetomakepath.cpp diff --git a/graph/minEdgetomakepath.cpp b/graph/minEdgetomakepath.cpp new file mode 100644 index 0000000..5ddd8d8 --- /dev/null +++ b/graph/minEdgetomakepath.cpp @@ -0,0 +1,43 @@ +#include +using namespace std; +void solve(int src,int dest,vector adj[],int n){ + vector> newAdj[n]; + for(int i=0;i,vector>,greater>> pq; + pq.push({0,src}); + vector cost(n,INT_MAX); + cost[src]=0; + while(!pq.empty()){ + auto pnt = pq.top().second;pq.pop(); + for(auto x:newAdj[pnt]){ + if(x.second+cost[pnt] < cost[x.first]){ + cost[x.first] = x.second+cost[pnt]; + pq.push({cost[x.first],x.first}); + } + } + } + cout<<"Minimum cost required "<>n>>e; + vector adj[n]; + for(int i=0;i>u>>v; + adj[u].push_back(v); + } + int src,dest; + cin>>src>>dest; + solve(src,dest,adj,n); + return 0; +} \ No newline at end of file