Repository navigation
Expand file tree
/
Copy pathBjSubway.java
More file actions
95 lines (79 loc) · 3.46 KB
/
Copy pathBjSubway.java
File metadata and controls
95 lines (79 loc) · 3.46 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
package wangkang;
import com.jwetherell.algorithms.data_structures.Graph;
import com.jwetherell.algorithms.graph.Dijkstra;
import java.util.*;
/**
* Author: helin <1006604973@qq.com>.
* Date: 13/11/2016 10:09
*/
public class BjSubway<T> {
static List<Station> stations = new ArrayList<>();
static List<MetroLine> metroLines = new ArrayList<>();
static Map<String, Integer> staNameMapToStaNumInAll = new HashMap<>();
static Map<Integer, String> staNumInAllMapToStaName = new HashMap<>();
static Map<Integer, Graph.Vertex> staNumInAllMapToVertex = new HashMap<>();
final List<Graph.Vertex<Integer>> verticies = new ArrayList<Graph.Vertex<Integer>>();
final List<Graph.Edge<Integer>> edges = new ArrayList<>();
Graph graph;
public Graph.Vertex getVertex(Integer staNumInAll) {
if (staNumInAllMapToVertex.containsKey(staNumInAll))
return staNumInAllMapToVertex.get(staNumInAll);
else {
Graph.Vertex vertex = new Graph.Vertex(staNumInAll);
staNumInAllMapToVertex.put(staNumInAll, vertex);
verticies.add(vertex);
return vertex;
}
}
private void graphInitByLine(MetroLine line) {
for (int i = 0; i < line.getStationList().size() - 1; i++) {
Integer staNumInAllOfFrom = line.getStationList().get(i).getStaNumInAll();
Graph.Vertex from = getVertex(staNumInAllOfFrom);
Integer staNumInAllOfTo = line.getStationList().get(i + 1).getStaNumInAll();
Graph.Vertex to = getVertex(staNumInAllOfTo);
Graph.Edge<Integer> edge = new Graph.Edge<>(
line.getDistanceList().get(i),
from,
to);
edges.add(edge);
}
}
public void graphInit() {
MetroBuilder.bulid();
metroLines.stream().forEach(line -> graphInitByLine(line));
graph = new Graph(verticies, edges);
}
// graph.getVertices().stream().forEach(
// o1 -> {
// System.out.println(((Graph.Vertex) o1).getValue());
// });
// graph.getEdges().stream().forEach(
// o -> {
// System.out.println(
// stations.get((Integer) ((Graph.Edge) o).getFromVertex().getValue()).getName() + " " +
// stations.get((Integer) ((Graph.Edge) o).getToVertex().getValue()).getName() + " " +
// ((Graph.Edge) o).getCost());
// });
public void path(String from, String to) {
if (from.equals("T3航站楼")) {
System.out.print("T3航站楼-->");
from = "T2航站楼";
}
Graph.Vertex vf = staNumInAllMapToVertex.get(staNameMapToStaNumInAll.get(from));
Graph.Vertex ve = staNumInAllMapToVertex.get(staNameMapToStaNumInAll.get(to));
Dijkstra.getShortestPath(graph, vf, ve).
getPath().
forEach(
o -> {
System.out.print(
staNumInAllMapToStaName.get(((Graph.Edge) o).getFromVertex().getValue()) + "-->"
);
});
System.out.println(to);
}
public static void main(String... args) {
BjSubway bjSubway = new BjSubway();
bjSubway.graphInit();
bjSubway.path("魏公村", "天宫院");
}
}