-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dijkstra_PriorityQueue.java
70 lines (52 loc) · 1.61 KB
/
Dijkstra_PriorityQueue.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
public class Dijkstra_PriorityQueue {
static class Vertex implements Comparable<Vertex>{
int no;
int distance;
Vertex(int no, int distance){
super();
this.no = no;
this.distance = distance;
}
@Override
public int compareTo(Vertex o) {
// TODO Auto-generated method stub
return this.distance - o.distance;
}
}
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
int[][] mat = new int[N][N];
for(int i=0;i<N;i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
for(int j =0;j<N;j++) {
mat[i][j] = Integer.parseInt(st.nextToken());
}
}
boolean[] visited = new boolean[N];
int[] distance = new int[N];
Arrays.fill(distance, Integer.MAX_VALUE);
int start = 0;
distance[start] = 0;
PriorityQueue<Vertex> pq = new PriorityQueue<>();
pq.offer(new Vertex(start,distance[start]));
while(!pq.isEmpty()) {
Vertex v = pq.poll();
if(visited[v.no]) continue;
visited[v.no] = true;
for(int i=0;i<N;i++) {
if(!visited[i] && mat[v.no][i] > 0 && distance[i] > v.distance + mat[v.no][i]) {
distance[i] = v.distance + mat[v.no][i];
pq.offer(new Vertex(i,distance[i]));
}
}
}
System.out.println(Arrays.toString(distance));
}
}