Skip to content

Commit

Permalink
Add implementation for finding minimum cost to make path b/w src to dest
Browse files Browse the repository at this point in the history
  • Loading branch information
nandwalritik committed Jul 21, 2021
1 parent 943c3ba commit ee2fb46
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions graph/minEdgetomakepath.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include<bits/stdc++.h>
using namespace std;
void solve(int src,int dest,vector<int> adj[],int n){
vector<pair<int,int>> newAdj[n];
for(int i=0;i<n;i++){
int u = i;
for(auto v:adj[i]){
newAdj[u].push_back({v,0});
newAdj[v].push_back({u,1});
}
}
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;
pq.push({0,src});
vector<int> 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 "<<cost[dest]<<endl;
return;

}
int main(){
int n,e;
// taking 0 based numbering for vertices of graph
cin>>n>>e;
vector<int> adj[n];
for(int i=0;i<e;i++){
int u,v;
cin>>u>>v;
adj[u].push_back(v);
}
int src,dest;
cin>>src>>dest;
solve(src,dest,adj,n);
return 0;
}

0 comments on commit ee2fb46

Please sign in to comment.