(int start, int end, List<double[]>[] graph)
| 35 | } |
| 36 | |
| 37 | private double dijkstra(int start, int end, List<double[]>[] graph){ |
| 38 | double[] proTo = new double[graph.length]; |
| 39 | // 初始化为一个去不到的值 |
| 40 | Arrays.fill(proTo, -1); |
| 41 | proTo[start] = 1; |
| 42 | |
| 43 | PriorityQueue<State> pq = new PriorityQueue<State>((a, b) -> { |
| 44 | return Double.compare(b.proToStart, a.proToStart); |
| 45 | }); |
| 46 | pq.offer(new State(start, 1)); |
| 47 | |
| 48 | while (!pq.isEmpty()){ |
| 49 | State cur = pq.poll(); |
| 50 | int curid = cur.id; |
| 51 | double curproToStart = cur.proToStart; |
| 52 | |
| 53 | if (curid == end) { |
| 54 | return curproToStart; |
| 55 | } |
| 56 | |
| 57 | if (proTo[curid] > curproToStart) { |
| 58 | continue; |
| 59 | } |
| 60 | |
| 61 | List<double[]> nexts = graph[curid]; |
| 62 | for (double[] next: nexts) { |
| 63 | double proToNext = proTo[curid] * next[1]; |
| 64 | int idx = (int) next[0]; |
| 65 | if (proToNext > proTo[idx]) { |
| 66 | proTo[idx] = proToNext; |
| 67 | pq.offer(new State(idx, proToNext)); |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | } |
no test coverage detected