Skip to content

Commit b66ec4d

Browse files
committed
feat: add solutions to lc problem: No.2093
No.2093.Minimum Cost to Reach City With Discounts
1 parent 773edfc commit b66ec4d

File tree

5 files changed

+258
-2
lines changed

5 files changed

+258
-2
lines changed

solution/2000-2099/2093.Minimum Cost to Reach City With Discounts/README.md

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,22 +68,111 @@
6868

6969
<!-- 这里可写通用的实现逻辑 -->
7070

71+
**方法一:BFS**
72+
73+
本题属于带限制的单源最短路问题。
74+
7175
<!-- tabs:start -->
7276

7377
### **Python3**
7478

7579
<!-- 这里可写当前语言的特殊实现逻辑 -->
7680

7781
```python
78-
82+
class Solution:
83+
def minimumCost(self, n: int, highways: List[List[int]], discounts: int) -> int:
84+
g = defaultdict(list)
85+
for a, b, c in highways:
86+
g[a].append((b, c))
87+
g[b].append((a, c))
88+
q = [(0, 0, 0)]
89+
dist = [[inf] * (discounts + 1) for _ in range(n)]
90+
while q:
91+
cost, i, k = heappop(q)
92+
if k > discounts:
93+
continue
94+
if i == n - 1:
95+
return cost
96+
if dist[i][k] > cost:
97+
dist[i][k] = cost
98+
for j, v in g[i]:
99+
heappush(q, (cost + v, j, k))
100+
heappush(q, (cost + v // 2, j, k + 1))
101+
return -1
79102
```
80103

81104
### **Java**
82105

83106
<!-- 这里可写当前语言的特殊实现逻辑 -->
84107

85108
```java
109+
class Solution {
110+
public int minimumCost(int n, int[][] highways, int discounts) {
111+
List<int[]>[] g = new List[n];
112+
for (int i = 0; i < n; ++i) {
113+
g[i] = new ArrayList<>();
114+
}
115+
for (var e : highways) {
116+
int a = e[0], b = e[1], c = e[2];
117+
g[a].add(new int[]{b, c});
118+
g[b].add(new int[]{a, c});
119+
}
120+
PriorityQueue<int[]> q = new PriorityQueue<>((a, b) -> a[0] - b[0]);
121+
q.offer(new int[]{0, 0, 0});
122+
int[][] dist = new int[n][discounts + 1];
123+
for (var e : dist) {
124+
Arrays.fill(e, Integer.MAX_VALUE);
125+
}
126+
while (!q.isEmpty()) {
127+
var p = q.poll();
128+
int cost = p[0], i = p[1], k = p[2];
129+
if (k > discounts || dist[i][k] <= cost) {
130+
continue;
131+
}
132+
if (i == n - 1) {
133+
return cost;
134+
}
135+
dist[i][k] = cost;
136+
for (int[] nxt : g[i]) {
137+
int j = nxt[0], v = nxt[1];
138+
q.offer(new int[]{cost + v, j, k});
139+
q.offer(new int[]{cost + v / 2, j, k + 1});
140+
}
141+
}
142+
return -1;
143+
}
144+
}
145+
```
86146

147+
### **C++**
148+
149+
```cpp
150+
class Solution {
151+
public:
152+
int minimumCost(int n, vector<vector<int>>& highways, int discounts) {
153+
vector<vector<pair<int, int>>> g(n);
154+
for (auto& e : highways) {
155+
int a = e[0], b = e[1], c = e[2];
156+
g[a].push_back({b, c});
157+
g[b].push_back({a, c});
158+
}
159+
priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<tuple<int, int, int>>> q;
160+
q.push({0, 0, 0});
161+
vector<vector<int>> dist(n, vector<int>(discounts + 1, INT_MAX));
162+
while (!q.empty()) {
163+
auto [cost, i, k] = q.top();
164+
q.pop();
165+
if (k > discounts || dist[i][k] <= cost) continue;
166+
if (i == n - 1) return cost;
167+
dist[i][k] = cost;
168+
for (auto [j, v] : g[i]) {
169+
q.push({cost + v, j, k});
170+
q.push({cost + v / 2, j, k + 1});
171+
}
172+
}
173+
return -1;
174+
}
175+
};
87176
```
88177
89178
### **TypeScript**

solution/2000-2099/2093.Minimum Cost to Reach City With Discounts/README_EN.md

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,98 @@ It is impossible to go from 0 to 3 so return -1.
6767
### **Python3**
6868

6969
```python
70-
70+
class Solution:
71+
def minimumCost(self, n: int, highways: List[List[int]], discounts: int) -> int:
72+
g = defaultdict(list)
73+
for a, b, c in highways:
74+
g[a].append((b, c))
75+
g[b].append((a, c))
76+
q = [(0, 0, 0)]
77+
dist = [[inf] * (discounts + 1) for _ in range(n)]
78+
while q:
79+
cost, i, k = heappop(q)
80+
if k > discounts:
81+
continue
82+
if i == n - 1:
83+
return cost
84+
if dist[i][k] > cost:
85+
dist[i][k] = cost
86+
for j, v in g[i]:
87+
heappush(q, (cost + v, j, k))
88+
heappush(q, (cost + v // 2, j, k + 1))
89+
return -1
7190
```
7291

7392
### **Java**
7493

7594
```java
95+
class Solution {
96+
public int minimumCost(int n, int[][] highways, int discounts) {
97+
List<int[]>[] g = new List[n];
98+
for (int i = 0; i < n; ++i) {
99+
g[i] = new ArrayList<>();
100+
}
101+
for (var e : highways) {
102+
int a = e[0], b = e[1], c = e[2];
103+
g[a].add(new int[]{b, c});
104+
g[b].add(new int[]{a, c});
105+
}
106+
PriorityQueue<int[]> q = new PriorityQueue<>((a, b) -> a[0] - b[0]);
107+
q.offer(new int[]{0, 0, 0});
108+
int[][] dist = new int[n][discounts + 1];
109+
for (var e : dist) {
110+
Arrays.fill(e, Integer.MAX_VALUE);
111+
}
112+
while (!q.isEmpty()) {
113+
var p = q.poll();
114+
int cost = p[0], i = p[1], k = p[2];
115+
if (k > discounts || dist[i][k] <= cost) {
116+
continue;
117+
}
118+
if (i == n - 1) {
119+
return cost;
120+
}
121+
dist[i][k] = cost;
122+
for (int[] nxt : g[i]) {
123+
int j = nxt[0], v = nxt[1];
124+
q.offer(new int[]{cost + v, j, k});
125+
q.offer(new int[]{cost + v / 2, j, k + 1});
126+
}
127+
}
128+
return -1;
129+
}
130+
}
131+
```
76132

133+
### **C++**
134+
135+
```cpp
136+
class Solution {
137+
public:
138+
int minimumCost(int n, vector<vector<int>>& highways, int discounts) {
139+
vector<vector<pair<int, int>>> g(n);
140+
for (auto& e : highways) {
141+
int a = e[0], b = e[1], c = e[2];
142+
g[a].push_back({b, c});
143+
g[b].push_back({a, c});
144+
}
145+
priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<tuple<int, int, int>>> q;
146+
q.push({0, 0, 0});
147+
vector<vector<int>> dist(n, vector<int>(discounts + 1, INT_MAX));
148+
while (!q.empty()) {
149+
auto [cost, i, k] = q.top();
150+
q.pop();
151+
if (k > discounts || dist[i][k] <= cost) continue;
152+
if (i == n - 1) return cost;
153+
dist[i][k] = cost;
154+
for (auto [j, v] : g[i]) {
155+
q.push({cost + v, j, k});
156+
q.push({cost + v / 2, j, k + 1});
157+
}
158+
}
159+
return -1;
160+
}
161+
};
77162
```
78163
79164
### **TypeScript**
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public:
3+
int minimumCost(int n, vector<vector<int>>& highways, int discounts) {
4+
vector<vector<pair<int, int>>> g(n);
5+
for (auto& e : highways) {
6+
int a = e[0], b = e[1], c = e[2];
7+
g[a].push_back({b, c});
8+
g[b].push_back({a, c});
9+
}
10+
priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<tuple<int, int, int>>> q;
11+
q.push({0, 0, 0});
12+
vector<vector<int>> dist(n, vector<int>(discounts + 1, INT_MAX));
13+
while (!q.empty()) {
14+
auto [cost, i, k] = q.top();
15+
q.pop();
16+
if (k > discounts || dist[i][k] <= cost) continue;
17+
if (i == n - 1) return cost;
18+
dist[i][k] = cost;
19+
for (auto [j, v] : g[i]) {
20+
q.push({cost + v, j, k});
21+
q.push({cost + v / 2, j, k + 1});
22+
}
23+
}
24+
return -1;
25+
}
26+
};
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution {
2+
public int minimumCost(int n, int[][] highways, int discounts) {
3+
List<int[]>[] g = new List[n];
4+
for (int i = 0; i < n; ++i) {
5+
g[i] = new ArrayList<>();
6+
}
7+
for (var e : highways) {
8+
int a = e[0], b = e[1], c = e[2];
9+
g[a].add(new int[]{b, c});
10+
g[b].add(new int[]{a, c});
11+
}
12+
PriorityQueue<int[]> q = new PriorityQueue<>((a, b) -> a[0] - b[0]);
13+
q.offer(new int[]{0, 0, 0});
14+
int[][] dist = new int[n][discounts + 1];
15+
for (var e : dist) {
16+
Arrays.fill(e, Integer.MAX_VALUE);
17+
}
18+
while (!q.isEmpty()) {
19+
var p = q.poll();
20+
int cost = p[0], i = p[1], k = p[2];
21+
if (k > discounts || dist[i][k] <= cost) {
22+
continue;
23+
}
24+
if (i == n - 1) {
25+
return cost;
26+
}
27+
dist[i][k] = cost;
28+
for (int[] nxt : g[i]) {
29+
int j = nxt[0], v = nxt[1];
30+
q.offer(new int[]{cost + v, j, k});
31+
q.offer(new int[]{cost + v / 2, j, k + 1});
32+
}
33+
}
34+
return -1;
35+
}
36+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
def minimumCost(self, n: int, highways: List[List[int]], discounts: int) -> int:
3+
g = defaultdict(list)
4+
for a, b, c in highways:
5+
g[a].append((b, c))
6+
g[b].append((a, c))
7+
q = [(0, 0, 0)]
8+
dist = [[inf] * (discounts + 1) for _ in range(n)]
9+
while q:
10+
cost, i, k = heappop(q)
11+
if k > discounts:
12+
continue
13+
if i == n - 1:
14+
return cost
15+
if dist[i][k] > cost:
16+
dist[i][k] = cost
17+
for j, v in g[i]:
18+
heappush(q, (cost + v, j, k))
19+
heappush(q, (cost + v // 2, j, k + 1))
20+
return -1

0 commit comments

Comments
 (0)