Repository navigation
Expand file tree
/
Copy pathDijkstra.java
More file actions
158 lines (125 loc) · 3.51 KB
/
Copy pathDijkstra.java
File metadata and controls
158 lines (125 loc) · 3.51 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import java.util.HashMap;
import java.util.PriorityQueue;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
class Vertex implements Comparable<Vertex> {
public final int name;
public HashMap<Integer, Edge> adjacencies = new HashMap<Integer, Edge> ();
public double minDistance = Double.POSITIVE_INFINITY;
public Vertex previous;
public Vertex(int argName) {
name = argName;
}
public int id() {
return name;
}
public int compareTo(Vertex other) {
return Double.compare(minDistance, other.minDistance);
}
public String toString() {
return "-- "+(name+1);
}
}
class Edge {
public final Vertex target;
public final Vertex start;
public final int weight;
public Edge(Vertex argStart, Vertex argTarget, int argWeight) {
target = argTarget;
weight = argWeight;
start = argStart;
}
}
public class Dijkstra {
public static void computePaths(Vertex source) {
source.minDistance = 0.;
PriorityQueue<Vertex> vertexQueue = new PriorityQueue<Vertex>();
vertexQueue.add(source);
while (!vertexQueue.isEmpty()) {
Vertex u = vertexQueue.poll();
// Visit each edge exiting u
for (Edge e : u.adjacencies.values()) {
Vertex v = e.target;
double weight = e.weight;
double distanceThroughU = u.minDistance + weight;
if (distanceThroughU < v.minDistance) {
vertexQueue.remove(v);
v.minDistance = distanceThroughU;
v.previous = u;
vertexQueue.add(v);
}
}
}
}
public static ArrayList<Vertex> getShortestPathTo(Vertex target) {
ArrayList<Vertex> path = new ArrayList<Vertex>();
for (Vertex vertex = target; vertex != null; vertex = vertex.previous)
path.add(vertex);
Collections.reverse(path);
return path;
}
public static void reset(ArrayList<Vertex> vertices, ArrayList<Vertex> path, Vertex d) {
HashMap<Integer, Edge> newAdj;
int i = 0;
for (Vertex v : vertices) {
v.minDistance = Double.POSITIVE_INFINITY;
v.previous = null;
newAdj = new HashMap<Integer, Edge>(v.adjacencies.size());
i = 0;
for (Edge e : v.adjacencies.values()) {
Vertex w = e.target;
if (!path.contains(w) || w.equals(d)) {
newAdj.put(e.target.id(), e);
}
}
v.adjacencies = newAdj;
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String[] a;
String line;
line = in.nextLine();
a = line.split("\\s+");
int N, R;
N = Integer.parseInt(a[0]);
R = Integer.parseInt(a[1]);
ArrayList<Vertex> vertices = new ArrayList<Vertex>(N+1);
for (int i = 0; i < N; i++) {
vertices.add(new Vertex(i));
}
for (int i = 0; i < R; i++) {
line = in.nextLine();
if(line.isEmpty()) continue;
a = line.split("\\s+");
int u = Integer.parseInt(a[0]);
int v = Integer.parseInt(a[1]);
int cost = Integer.parseInt(a[2]);
vertices.get(u-1).adjacencies.put(v-1, new Edge(vertices.get(u-1), vertices
.get(v-1), cost));
}
int s = 1;
int d = N;
int sum =0;
computePaths(vertices.get(s-1));
ArrayList<Vertex> path = getShortestPathTo(vertices.get(d-1));
for(int i=0; i<path.size()-1; i++){
Vertex u = path.get(i);
Vertex v = path.get(i+1);
Edge e = u.adjacencies.get(v.id());
sum += e.weight;
}
reset(vertices, path, vertices.get(d-1));
computePaths(vertices.get(s-1));
path = getShortestPathTo(vertices.get(d-1));
for(int i=0; i<path.size()-1; i++){
Vertex u = path.get(i);
Vertex v = path.get(i+1);
Edge e = u.adjacencies.get(v.id());
sum += e.weight;
}
System.out.println(sum);
}
}