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

Commit 0313b39

Browse files
feat: add solutions to lc problem: No.1514 (doocs#3816)
No.1514.Path with Maximum Probability
1 parent f963418 commit 0313b39

File tree

16 files changed

+405
-591
lines changed

16 files changed

+405
-591
lines changed

‎solution/1500-1599/1514.Path with Maximum Probability/README.md‎

Lines changed: 133 additions & 201 deletions
Large diffs are not rendered by default.

‎solution/1500-1599/1514.Path with Maximum Probability/README_EN.md‎

Lines changed: 135 additions & 199 deletions
Large diffs are not rendered by default.
Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
class Solution {
22
public:
3-
double maxProbability(int n, vector<vector<int>>& edges, vector<double>& succProb, int start, int end) {
4-
vector<vector<pair<int, double>>> g(n);
3+
double maxProbability(int n, vector<vector<int>>& edges, vector<double>& succProb, int start_node, int end_node) {
4+
using pdi = pair<double, int>;
5+
vector<pdi> g[n];
56
for (int i = 0; i < edges.size(); ++i) {
67
int a = edges[i][0], b = edges[i][1];
7-
double s = succProb[i];
8-
g[a].push_back({b, s});
9-
g[b].push_back({a, s});
8+
double p = succProb[i];
9+
g[a].emplace_back(p, b);
10+
g[b].emplace_back(p, a);
1011
}
11-
vector<double> d(n);
12-
d[start] = 1.0;
13-
queue<pair<double, int>> q;
14-
q.push({1.0, start});
15-
while (!q.empty()) {
16-
auto p = q.front();
17-
q.pop();
18-
double w = p.first;
19-
int u = p.second;
20-
if (d[u] > w) continue;
21-
for (auto& e : g[u]) {
22-
int v = e.first;
23-
double t = e.second;
24-
if (d[v] < d[u] * t) {
25-
d[v] = d[u] * t;
26-
q.push({d[v], v});
12+
vector<double> dist(n);
13+
dist[start_node] = 1;
14+
priority_queue<pdi> pq;
15+
pq.emplace(1, start_node);
16+
while (!pq.empty()) {
17+
auto [w, a] = pq.top();
18+
pq.pop();
19+
if (dist[a] > w) {
20+
continue;
21+
}
22+
for (auto [p, b] : g[a]) {
23+
auto nw = w * p;
24+
if (nw > dist[b]) {
25+
dist[b] = nw;
26+
pq.emplace(nw, b);
2727
}
2828
}
2929
}
30-
return d[end];
30+
return dist[end_node];
3131
}
32-
};
32+
};
Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,39 @@
1-
func maxProbability(n int, edges [][]int, succProb []float64, start int, end int) float64 {
1+
func maxProbability(n int, edges [][]int, succProb []float64, start_node int, end_node int) float64 {
22
g := make([][]pair, n)
33
for i, e := range edges {
4-
a, b, s := e[0], e[1], succProb[i]
5-
g[a] = append(g[a], pair{b, s})
6-
g[b] = append(g[b], pair{a, s})
4+
a, b := e[0], e[1]
5+
p := succProb[i]
6+
g[a] = append(g[a], pair{p, b})
7+
g[b] = append(g[b], pair{p, a})
78
}
8-
d := make([]float64, n)
9-
d[start] = 1
10-
vis := make([]bool, n)
11-
q := []int{start}
12-
vis[start] = true
13-
for len(q) > 0 {
14-
i := q[0]
15-
q = q[1:]
16-
vis[i] = false
17-
for _, ne := range g[i] {
18-
j, s := ne.idx, ne.s
19-
if d[j] < d[i]*s {
20-
d[j] = d[i] * s
21-
if !vis[j] {
22-
q = append(q, j)
23-
vis[j] = true
24-
}
9+
pq := hp{{1, start_node}}
10+
dist := make([]float64, n)
11+
dist[start_node] = 1
12+
for len(pq) > 0 {
13+
p := heap.Pop(&pq).(pair)
14+
w, a := p.p, p.a
15+
if dist[a] > w {
16+
continue
17+
}
18+
for _, e := range g[a] {
19+
b, p := e.a, e.p
20+
if nw := w * p; nw > dist[b] {
21+
dist[b] = nw
22+
heap.Push(&pq, pair{nw, b})
2523
}
2624
}
2725
}
28-
return d[end]
26+
return dist[end_node]
2927
}
3028

3129
type pair struct {
32-
idx int
33-
s float64
34-
}
30+
p float64
31+
a int
32+
}
33+
type hp []pair
34+
35+
func (h hp) Len() int { return len(h) }
36+
func (h hp) Less(i, j int) bool { return h[i].p > h[j].p }
37+
func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
38+
func (h *hp) Push(x any) { *h = append(*h, x.(pair)) }
39+
func (h *hp) Pop() (x any) { a := *h; x = a[len(a)-1]; *h = a[:len(a)-1]; return }
Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,37 @@
11
class Solution {
2-
public double maxProbability(int n, int[][] edges, double[] succProb, int start, int end) {
2+
public double maxProbability(
3+
int n, int[][] edges, double[] succProb, int start_node, int end_node) {
34
List<Pair<Integer, Double>>[] g = new List[n];
45
Arrays.setAll(g, k -> new ArrayList<>());
56
for (int i = 0; i < edges.length; ++i) {
6-
int a = edges[i][0], b = edges[i][1];
7-
double s = succProb[i];
8-
g[a].add(new Pair<>(b, s));
9-
g[b].add(new Pair<>(a, s));
7+
var e = edges[i];
8+
int a = e[0], b = e[1];
9+
double p = succProb[i];
10+
g[a].add(new Pair<>(b, p));
11+
g[b].add(new Pair<>(a, p));
1012
}
11-
PriorityQueue<Pair<Double, Integer>> q
12-
= new PriorityQueue<>(Comparator.comparingDouble(Pair::getKey));
13-
double[] d = new double[n];
14-
d[start] = 1.0;
15-
q.offer(new Pair<>(-1.0, start));
16-
while (!q.isEmpty()) {
17-
Pair<Double, Integer> p = q.poll();
18-
double w = p.getKey();
19-
w *= -1;
20-
int u = p.getValue();
21-
for (Pair<Integer, Double> ne : g[u]) {
22-
int v = ne.getKey();
23-
double t = ne.getValue();
24-
if (d[v] < d[u] * t) {
25-
d[v] = d[u] * t;
26-
q.offer(new Pair<>(-d[v], v));
13+
double[] dist = new double[n];
14+
dist[start_node] = 1;
15+
PriorityQueue<Pair<Integer, Double>> pq
16+
= new PriorityQueue<>(Comparator.comparingDouble(p -> - p.getValue()));
17+
pq.offer(new Pair<>(start_node, 1.0));
18+
while (!pq.isEmpty()) {
19+
var p = pq.poll();
20+
int a = p.getKey();
21+
double w = p.getValue();
22+
if (dist[a] > w) {
23+
continue;
24+
}
25+
for (var e : g[a]) {
26+
int b = e.getKey();
27+
double pab = e.getValue();
28+
double wab = w * pab;
29+
if (wab > dist[b]) {
30+
dist[b] = wab;
31+
pq.offer(new Pair<>(b, wab));
2732
}
2833
}
2934
}
30-
return d[end];
35+
return dist[end_node];
3136
}
32-
}
37+
}

‎solution/1500-1599/1514.Path with Maximum Probability/Solution.py‎

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,23 @@ def maxProbability(
44
n: int,
55
edges: List[List[int]],
66
succProb: List[float],
7-
start: int,
8-
end: int,
7+
start_node: int,
8+
end_node: int,
99
) -> float:
10-
g=defaultdict(list)
11-
for (a, b), s in zip(edges, succProb):
12-
g[a].append((b, s))
13-
g[b].append((a, s))
14-
q = [(-1, start)]
15-
d = [0] * n
16-
d[start] = 1
17-
while q:
18-
w, u = heappop(q)
10+
g: List[List[Tuple[int, float]]] = [[] for_inrange(n)]
11+
for (a, b), p in zip(edges, succProb):
12+
g[a].append((b, p))
13+
g[b].append((a, p))
14+
pq = [(-1, start_node)]
15+
dist = [0] * n
16+
dist[start_node] = 1
17+
while pq:
18+
w, a = heappop(pq)
1919
w = -w
20-
if d[u] > w:
20+
if dist[a] > w:
2121
continue
22-
for v, t in g[u]:
23-
if d[v] <d[u] * t:
24-
d[v] =d[u] * t
25-
heappush(q, (-d[v], v))
26-
return d[end]
22+
for b, p in g[a]:
23+
if (t:=w * p) >dist[b]:
24+
dist[b] = t
25+
heappush(pq, (-t, b))
26+
return dist[end_node]
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
function maxProbability(
2+
n: number,
3+
edges: number[][],
4+
succProb: number[],
5+
start_node: number,
6+
end_node: number,
7+
): number {
8+
const pq = new MaxPriorityQueue({ priority: v => v[0] });
9+
const g: [number, number][][] = Array.from({ length: n }, () => []);
10+
for (let i = 0; i < edges.length; ++i) {
11+
const [a, b] = edges[i];
12+
g[a].push([b, succProb[i]]);
13+
g[b].push([a, succProb[i]]);
14+
}
15+
const dist = Array.from({ length: n }, () => 0);
16+
dist[start_node] = 1;
17+
pq.enqueue([1, start_node]);
18+
while (!pq.isEmpty()) {
19+
const [w, a] = pq.dequeue().element;
20+
if (dist[a] > w) {
21+
continue;
22+
}
23+
for (const [b, p] of g[a]) {
24+
const nw = w * p;
25+
if (nw > dist[b]) {
26+
dist[b] = nw;
27+
pq.enqueue([nw, b]);
28+
}
29+
}
30+
}
31+
return dist[end_node];
32+
}

‎solution/1500-1599/1514.Path with Maximum Probability/Solution2.cpp‎

Lines changed: 0 additions & 34 deletions
This file was deleted.

‎solution/1500-1599/1514.Path with Maximum Probability/Solution2.java‎

Lines changed: 0 additions & 34 deletions
This file was deleted.

‎solution/1500-1599/1514.Path with Maximum Probability/Solution2.py‎

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /