-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLeetcode1514.java
54 lines (50 loc) · 1.89 KB
/
Leetcode1514.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
54
public class Leetcode1514 {
class State{
int id;
double probFromStart;
public State(int id, double probFromStart){
this.id = id;
this.probFromStart = probFromStart;
}
}
private double dijkstra(int start, int end,List<double[]>[] graph){
double[] probTo = new double[graph.length];
Arrays.fill(probTo,-1);
probTo[start] = 1;
Queue<State> pq = new PriorityQueue<>((a,b)->{
return Double.compare(b.probFromStart,a.probFromStart);
});
pq.offer(new State(start,1));
while(!pq.isEmpty()){
State curState = pq.poll();
int curId = curState.id;
double curProbFromStart = curState.probFromStart;
if(curId == end)return curProbFromStart;
if(curProbFromStart < probTo[curId])continue;
for(double[] neighbor:graph[curId]){
int nextNodeId = (int)neighbor[0];
double probToNext = probTo[curId]*neighbor[1];
if(probTo[nextNodeId] < probToNext){
probTo[nextNodeId] = probToNext;
pq.offer(new State(nextNodeId,probToNext));
}
}
}
// if we reach this statement it means end cannot be reached.
return 0.0;
}
public double maxProbability(int n, int[][] edges, double[] succProb, int start, int end) {
List<double[]>[] graph = new LinkedList[n];
for(int i = 0;i < n;i++){
graph[i] = new LinkedList<>();
}
for(int i = 0;i < edges.length;i++){
int from = edges[i][0];
int to = edges[i][1];
double weight = succProb[i];
graph[from].add(new double[]{to,weight});
graph[to].add(new double[]{from,weight});
}
return dijkstra(start,end,graph);
}
}