Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit04d4418

Browse files
authored
Merge pull requestneetcode-gh#3264 from adityaiiitr/adityacpp
create 1091-shortestpath-in-binary-matrix.cpp
2 parents779349c +6092475 commit04d4418

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

‎cpp/0787-cheapest-flights-within-k-stops.cpp

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,40 @@
1+
/**
2+
This function uses the Bellman-Ford algorithm to find the cheapest price from source (src) to destination (dst)
3+
with at most k stops allowed. It iteratively relaxes the edges for k+1 iterations, updating the minimum
4+
cost to reach each vertex. The final result is the minimum cost to reach the destination, or -1 if the
5+
destination is not reachable within the given constraints.
6+
7+
Space Complexity: O(n) - space used for the prices array.
8+
Time Complexity: O(k * |flights|) - k iterations, processing all flights in each iteration.
9+
*/
10+
classSolution {
11+
public:
12+
intfindCheapestPrice(int n, vector<vector<int>>& flights,int src,int dst,int k) {
13+
vector<int>prices(n, INT_MAX);
14+
prices[src] =0;
15+
16+
// Perform k+1 iterations of Bellman-Ford algorithm.
17+
for (int i =0; i < k +1; i++) {
18+
vector<int>tmpPrices(begin(prices),end(prices));
19+
20+
for (auto it : flights) {
21+
int s = it[0];
22+
int d = it[1];
23+
int p = it[2];
24+
25+
if (prices[s] == INT_MAX)continue;
26+
27+
if (prices[s] + p < tmpPrices[d]) {
28+
tmpPrices[d] = prices[s] + p;
29+
}
30+
}
31+
prices = tmpPrices;
32+
}
33+
return prices[dst] == INT_MAX ? -1 : prices[dst];
34+
}
35+
};
36+
37+
138
/*
239
Given cities connected by flights [from,to,price], also given src, dst, & k:
340
Return cheapest price from src to dst with at most k stops
@@ -75,4 +112,4 @@ class Solution {
75112
}
76113
return distances[dst];
77114
}
78-
};
115+
};
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
classSolution {
2+
public:
3+
intshortestPathBinaryMatrix(vector<vector<int>>& grid) {
4+
int n = grid.size();
5+
if(grid[0][0]==1 || grid[n-1][n-1]==1)return -1;
6+
7+
// {{r,c},length}
8+
queue<pair<pair<int,int>,int>> q;
9+
set<pair<int,int>> visited;
10+
11+
q.push({{0,0},0});
12+
visited.insert(make_pair(0,0));
13+
14+
int drow[] = {0,1,0,-1,1,-1,1,-1};
15+
int dcol[] = {1,0,-1,0,1,-1,-1,1};
16+
17+
while(!q.empty()){
18+
int row = q.front().first.first;
19+
int col = q.front().first.second;
20+
int len = q.front().second;
21+
22+
if(row == n-1 && col == n-1){
23+
return len+1;
24+
}
25+
26+
for(int i=0; i<8; i++){
27+
int nrow = row + drow[i];
28+
int ncol = col + dcol[i];
29+
30+
if(nrow>=0 && nrow<n && ncol>=0 && ncol<n && visited.find({nrow,ncol})==visited.end() && grid[nrow][ncol]==0){
31+
q.push({{nrow,ncol},len+1});
32+
visited.insert(make_pair(nrow,ncol));
33+
}
34+
}
35+
q.pop();
36+
}
37+
return -1;
38+
}
39+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp