第一次结对作业5
整体思路:利用邻接矩阵将北京地铁线路图存储,利用Floyd算法求出所有站点间的最短路径。
本题要求的是:查询两点间的线路时换乘线路最少,本算法求得的是经过站点最少的线路,虽然不是严格符合题目要求,但是在绝大部分情况下经过站点最少的线路同样是换成线路最少。
两站查询效果如下:
本系统的主要代码如下:
package com.example.subwayUtil; import java.util.ArrayList; import java.util.List; public class Floyd { private static final int max = 99999; private int[][] path; //最短路径 private int[][] distance; //最短距离 //Floyd算法求最短路径 public Floyd(int[][] Graph){ this.path = new int[Graph.length][Graph.length]; this.distance = new int[Graph.length][Graph.length]; for(int i = 0; i < Graph.length; i++) for(int j = 0; j < Graph.length; j++) { this.path[i][j] = j; this.distance[i][j] = Graph[i][j]; this.distance[j][i] = Graph[j][i]; } for(int k = 0; k < Graph.length; k++) for(int i = 0; i < Graph.length; i++) for(int j = 0; j < Graph.length; j++) if(this.distance[i][j] > this.distance[i][k] + this.distance[k][j]) { this.distance[i][j] = this.distance[i][k] + this.distance[k][j]; this.path[i][j] = this.path[i][k]; } } //查找最小距离 public int SearchMin(int i, int j){ return this.distance[i][j]; } public static int getMax() { return max; } public int[][] getPath() { return path; } public void setPath(int[][] path) { this.path = path; } public int[][] getDistance() { return distance; } public void setDistance(int[][] distance) { this.distance = distance; } //将最短距离的站点下标加入到列表中 public List<Integer> printPath(int i, int j){ List<Integer> list = new ArrayList<>(); while(i != j){ System.out.println(i + " " + j); list.add(i); i = this.path[i][j]; // list.add(i); } list.add(i); return list; } } package com.example.subwayUtil; import java.util.ArrayList; import java.util.List; public class Line { private String lineName; //存储站点所在线路名 private List<String> stations = new ArrayList<>(); //存储线路内所有站点名 public String getLineName() { return lineName; } public void setLineName(String lineName) { this.lineName = lineName; } public List<String> getStations() { return stations; } public void setStations(List<String> stations) { this.stations = stations; } @Override public String toString() { return "Station{" + "stationNum='" + lineName + '\'' + ", stations=" + stations + '}'; } } package com.example.subwayUtil; import com.example.domain.Stopinfo; import java.util.ArrayList; import java.util.HashSet; import java.util.List; public class SearchUtil { public static List<String> searchNoInOneLine(List<Stopinfo> allStop, String stop1, String stop2){ System.out.println(allStop); // 创建所有线路列表 List<Line> lines = new ArrayList<>(); for (Stopinfo stopinfo : allStop) { int flag = 0; for (Line line : lines) { if (line.getLineName().equals(stopinfo.getLineName())){ line.getStations().add(stopinfo.getStopName()); flag = 1; } } if (flag == 0){ Line line = new Line(); line.setLineName(stopinfo.getLineName()); line.getStations().add(stopinfo.getStopName()); lines.add(line); } } System.out.println("\n\n\n\nlog\n\n\n"); for (Line line : lines) { System.out.println(line.getLineName()); for (String station : line.getStations()) { System.out.print(station + " "); } } // 存储所有站点 List<String> allStations = new ArrayList<>(); for (Line line : lines) { allStations.addAll(line.getStations()); } // 去重 HashSet hashSet = new HashSet(allStations); allStations = new ArrayList<>(hashSet); Subway subway = new Subway(allStations); // 添加线路图中的边 for (Line item : lines) { List<String> line = item.getStations(); if (item.getLineName().equals("10号线")) subway.relation(line.get(0), line.get(line.size()-1)); for (int i = 0; i < line.size()-1; i++) { subway.relation(line.get(i), line.get(i+1)); } } Floyd floyd = new Floyd(subway.getSubwayMatrix()); subway.setPath(floyd.getPath()); System.out.println("\n\n\n"+stop1+" "+stop2+"\n\n\n"); List<Integer> path = floyd.printPath(subway.getSite(stop1), subway.getSite(stop2)); List<String> pathName = new ArrayList<>(); for (Integer idx : path) { pathName.add(subway.getName(idx)); } return pathName; } } package com.example.subwayUtil; import java.util.List; public class Subway { private int[][] subwayMatrix; //存储地铁线路 private List<String> stationName; //存储所有站点名 private int[][] path; //存储两站点间路径 private static final int max = 99999; //最大距离,两站点间不可达 //初始化地铁线路图 public Subway(List<String> stationName){ System.out.println("\n\n\n\n" + stationName.size() + "\n\n\n\n"); this.stationName = stationName; this.subwayMatrix = new int[stationName.size()][stationName.size()]; this.path = new int[stationName.size()][stationName.size()]; for(int i = 0; i < stationName.size(); i++){ for(int j = 0; j < stationName.size(); j++){ if(i == j) subwayMatrix[i][j] = 0; else { subwayMatrix[i][j] = max; subwayMatrix[j][i] = max; } } } } public int getSite(String station){ return stationName.indexOf(station); } public String getName(int site){ return stationName.get(site); } //初始化相邻地铁站之间距离为1 public void relation(String start, String end){ int i = getSite(start); int j = getSite(end); this.subwayMatrix[i][j] = 1; this.subwayMatrix[j][i] = 1; } public int[][] getSubwayMatrix() { return subwayMatrix; } public void setSubwayMatrix(int[][] subwayMatrix) { this.subwayMatrix = subwayMatrix; } public List<String> getStationName() { return stationName; } public void setStationName(List<String> stationName) { this.stationName = stationName; } public int[][] getPath() { return path; } public void setPath(int[][] path) { this.path = path; } public static int getMax() { return max; } }
本文作者:lmyyyy
本文链接:https://www.cnblogs.com/lmyy/p/17232000.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步