-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDay46.java
More file actions
55 lines (48 loc) · 1.72 KB
/
Day46.java
File metadata and controls
55 lines (48 loc) · 1.72 KB
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
55
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.List;
import java.util.ArrayList;
class Pair implements Comparable<Pair> {
int vertex;
int distance;
public Pair(int vertex, int distance) {
this.vertex = vertex;
this.distance = distance;
}
@Override
public int compareTo(Pair other) {
return Integer.compare(this.distance, other.distance);
}
}
public class Day46 {
public static List<Integer> shortestPath(int V, List<List<int[]>> adj, int S) {
List<Integer> distances = new ArrayList<>();
PriorityQueue<Pair> minHeap = new PriorityQueue<>();
// Initialize distances with infinity and the source vertex with 0
Arrays.fill(distances.toArray(), Integer.MAX_VALUE);
distances.set(S, 0);
minHeap.offer(new Pair(S, 0));
while (!minHeap.isEmpty()) {
Pair current = minHeap.poll();
for (int[] neighbor : adj.get(current.vertex)) {
int nextVertex = neighbor[0];
int edgeWeight = neighbor[1];
int newDistance = current.distance + edgeWeight;
if (newDistance < distances.get(nextVertex)) {
distances.set(nextVertex, newDistance);
minHeap.offer(new Pair(nextVertex, newDistance));
}
}
}
return distances;
}
public static void main(String[] args) {
int V = 2;
List<List<int[]>> adj = new ArrayList<>();
adj.add(Arrays.asList(new int[]{1, 9}));
adj.add(Arrays.asList(new int[]{0, 9}));
int S = 0;
List<Integer> result = shortestPath(V, adj, S);
System.out.println(result); // Output: [0, 9]
}
}