Repository navigation
Expand file tree
/
Copy pathminPath.java
More file actions
86 lines (72 loc) · 2.81 KB
/
Copy pathminPath.java
File metadata and controls
86 lines (72 loc) · 2.81 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
import java.util.*;
//import Map.Graph;
public class minPath {
static int Inf = Integer.MAX_VALUE;
public static double[] dijkstra(Map.Graph graph, int source, int go_to, int V, int priority) {
boolean[] visited = new boolean[V];
double[] distance = new double[V];
double[] time = new double[V];
int[] parent = new int[V];
for (int i = 0; i < V; i++) {
visited[i] = false;
distance[i] = Inf;
time[i] = Inf;
}
// Distance of source is zero
distance[source] = 0;
time[source] = 0;
parent[0] = -1;
for (int i = 0; i < V; i++) {
// Update the distance between neighbouring vertex and source vertex
int u = findMinDistance(distance, visited);
visited[u] = true;
LinkedList<Map.Edge> list = graph.adjacencylist[u];
// Update all the neighbouring vertex distances
for (int j = 0; j < list.size(); j++) {
if (priority == 0) {
if ((!visited[list.get(j).destination])
&& (distance[u] + list.get(j).weight[priority] < distance[list.get(j).destination])) {
distance[list.get(j).destination] = distance[u] + list.get(j).weight[priority];
time[list.get(j).destination] = time[u] + list.get(j).weight[1];
parent[list.get(j).destination] = u;
}
}
else if (priority == 1) {
if ((!visited[list.get(j).destination])
&& (time[u] + list.get(j).weight[priority] < time[list.get(j).destination])) {
time[list.get(j).destination] = time[u] + list.get(j).weight[priority];
distance[list.get(j).destination] = distance[u] + list.get(j).weight[0];
parent[list.get(j).destination] = u;
}
}
}
if (u == go_to)
break;
}
return new double[] { distance[go_to], time[go_to] };
}
// ---------------------------------------------------------------------------------------------------------------//
// Finding the minimum distance
private static int findMinDistance(double[] distance, boolean[] visited) {
double minDistance = Inf;
int minDistanceVertex = 0;
for (int i = 0; i < distance.length; i++) {
if (!visited[i] && distance[i] < minDistance) {
minDistance = distance[i];
minDistanceVertex = i;
}
}
return minDistanceVertex;
}
// ---------------------------------------------------------------------------------------------------------------//
// Finding the cost of the fare
public static double findCost(double tripDistance, int choice) {
//cab types = "Auto", "Mini", "Macro", "Prime SUV"
int mileage[] = { 35, 18, 14, 10 };
int fuelPrice = 107;
double reqFuel = tripDistance / mileage[choice];
double fuelCost = fuelPrice * reqFuel;
double finalFare = fuelCost * 100 / 30;
return finalFare;
}
}