-
Notifications
You must be signed in to change notification settings - Fork 0
/
DijkstraToll.java
86 lines (73 loc) · 2.8 KB
/
DijkstraToll.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import java.util.ArrayList;
import java.util.List;
public class DijkstraToll extends Algorithm{
DijkstraToll(Board b) {
super(b);
}
Cities myCities = b.myCities;
private List<City> unvisited= new ArrayList<City>();
private CostPath[] tollPath = new CostPath[myCities.cities.size()];
public void shortestPath(City start, City end) {
// Add all cities to the unvisited list
unvisited = new ArrayList<>(myCities.cities);
// Initialize all distances to infinity
for (int i =0 ; i < myCities.cityCount(); i++)
{
tollPath[i] = new CostPath(Integer.MAX_VALUE, new ArrayList<City>());
tollPath[i].path.add(start);
}
// Set the distance to the start city to 0
tollPath[start.index].cost = 0;
unvisited.remove(start);
long startTime = System.nanoTime();
iterateShortestPath(start, end);
long endTime = System.nanoTime();
List<String> path = new ArrayList<>();
for (City c: tollPath[end.index].path) {
path.add(c.name);
}
System.out.println("Smallest Toll Price: " + tollPath[end.index].cost + " : " + path);
System.out.println("This Path will cost : " + getTrainCost(end) + " train(s)");
System.out.println("Dijkstra Smallest Toll Price calculation time: " + (endTime-startTime) + "ns");
}
public void iterateShortestPath(City start, City end) {
// Update the shortest path to all cities reachable from start
for (City c : unvisited)
{
int cost = b.getBridgeToll(start, c);
if (cost > 0)
{
if (tollPath[c.index].cost > cost + tollPath[start.index].cost)
{
tollPath[c.index].cost = cost + tollPath[start.index].cost;
tollPath[c.index].path = new ArrayList<City>(tollPath[start.index].path);
tollPath[c.index].path.add(c);
}
}
}
// Find the next city to visit
City next = unvisited.get(0); // Need to give it a possible value
for (City c : unvisited)
{
if (tollPath[c.index].cost < tollPath[next.index].cost)
{
next = c;
}
}
unvisited.remove(next);
// If the next city is the end city we are done
if (next == end)
{
return;
}
iterateShortestPath(next, end);
}
public int getTrainCost( City end){
int cost = 0;
//Get the bridge toll from the board add that value to int cost
for(int i = 0; i <= (tollPath[end.index].path.size() - 2) ; i++){
cost += b.getBoard(tollPath[end.index].path.get(i), tollPath[end.index].path.get(i+1));
}
return cost;
}
}