Skip to content

Commit

Permalink
Create MinCostPath.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
kshitiz11101 authored Sep 2, 2024
1 parent 60b63d4 commit 34afccc
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions Graphs/MinCostPath.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@


class Solution
{
public:
//Function to return the minimum cost to react at bottom
//right cell from top left cell.
int minimumCostPath(vector<vector<int>>& grid)
{
// Code here
int n=grid.size();
priority_queue<pair<int,pair<int,int>>,vector<pair<int,pair<int,int>>>,greater<pair<int,pair<int,int>>>>pq;
pq.push({grid[0][0],{0,0}});
int dr[]={-1,0,0,1};
int dc[]={0,-1,1,0};
vector<vector<int>>dist(n,vector<int>(n,1e9));
dist[0][0]=grid[0][0];
while(!pq.empty()){
int dis=pq.top().first;
int row=pq.top().second.first;
int col=pq.top().second.second;
pq.pop();
for(int i=0;i<4;i++){
int nrow=row+dr[i];
int ncol=col+dc[i];
if(nrow>=0 && nrow<n && ncol>=0 && ncol<n){
if(dis+grid[nrow][ncol]<dist[nrow][ncol]){
dist[nrow][ncol]=dis+grid[nrow][ncol];
pq.push({dist[nrow][ncol],{nrow,ncol}});
}
}
}
}
return dist[n-1][n-1];
}
};

0 comments on commit 34afccc

Please sign in to comment.