-
Notifications
You must be signed in to change notification settings - Fork 0
/
PathWithMaximumProbability_1514.java
53 lines (49 loc) · 1.88 KB
/
PathWithMaximumProbability_1514.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package org.example;
import java.util.*;
public class PathWithMaximumProbability_1514 {
class Solution {
public double maxProbability(int n, int[][] edges, double[] succProb, int start, int end) {
double[] dist = new double[n];
dist[start]=1;
PriorityQueue<double[]> queue = new PriorityQueue<>(new Comparator<double[]>() {
@Override
public int compare(double[] o1, double[] o2) {
if (o1[1]==o2[1] ){
return 0;
}
return o2[1]>o1[1] ? 1 : -1;
}
});
Map<Integer, Double>[] map = new Map[n];
for (int i = 0; i < n; i++) {
map[i] = new HashMap<>();
}
for (int i=0; i<edges.length;i++){
int[] edge = edges[i];
map[edge[0]].put(edge[1], succProb[i]);
map[edge[1]].put(edge[0], succProb[i]);
}
// Set<Integer> set = new HashSet<>();
queue.offer(new double[]{start, 1});
// set.remove(start);
while (queue.size()>0){
double[] idx = queue.poll();
if (idx[0] == end){
return dist[end];
}
Map<Integer, Double> dest = map[(int) idx[0]];
if (dest!=null){
Set<Map.Entry<Integer, Double>> set = dest.entrySet();
for (Map.Entry<Integer, Double> d:set){
double newDist = d.getValue()*dist[(int) idx[0]];
if (newDist>dist[d.getKey()]){
dist[d.getKey()] = newDist;
queue.offer(new double[]{d.getKey(),newDist});
}
}
}
}
return 0;
}
}
}