北京地铁线路图
Github
https://github.com/31701014/Subway
一、功能
读取一定格式的地铁线路txt文件
根据用户需求提供最优乘坐方案或地铁线路信息
二、文件格式
三、测试
查询线路:
查询最短路径:
错误输入:
四、算法设计
数据读入存储:
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("E:/subway.txt"))));
String lineTxt = null;
while ((lineTxt = br.readLine()) != null) {
String na=null;
String[] names = lineTxt.split(" ");
String iid=names[0];
for(int i=0;i
站点及线路信息均保存在一维数组Map中
最短路径算法:
static double Dijkstra(double Map[], int path[], int n, int v, int d) {
double min, ks;
int u, t;
double dist[] = new double[n + 1];
int s[] = new int[n + 1];
for (int i = 0; i < n; i++) {
dist[i] = Map[n * v + i];
s[i] = 0;
if (i != v && dist[i] < MaxInt) {
path[i] = v;
} else
path[i] = -1;
}
s[v] = 1;
for (int i = 0; i < n - 1; i++) {
min = MaxInt;
u = v;
for (int j = 0; j < n; j++) {
if (s[j] == 0 && dist[j] < min) {
u = j;
min = dist[j];
}
}
s[u] = 1;
for (t = 0; t < n; t++) {
if (s[t] == 0 && dist[u] + Map[n * u + t] < dist[t]) {
dist[t] = (float) (dist[u] + Map[n * u + t]);
path[t] = u;
}
}
}
ks = dist[d];// 现在只返回人要的那个目的值
return ks;
} // Dijkstra算法内容
输出及交互:
String start, dest,road;
double race = 21;
System.out.print("请输入操作(查询线路为a,查询最短路径为b):");
Scanner cin = new Scanner(System.in);
String temp=cin.nextLine();
if(temp.equals("a")) {
System.out.print("请输入查询线路:");
road = cin.nextLine();
try {
BufferedReader bw = new BufferedReader(new InputStreamReader(new FileInputStream(new File("E:/subway.txt"))));
String line = null;
int have=0;
while ((line = bw.readLine()) != null) {
String[] names = line.split(" ");
String iid=names[0];
if(iid.equals(road)) {
have=1;
for (String namer : names) {
if(id.get(namer)!=null) {
System.out.println(namer+"("+id.get(namer)+")");
}
}
}
}
if(have==0) System.out.println("线路不存在");
bw.close();
}catch (Exception e) {
System.err.println("错误");
}
}
else if(temp.equals("b")) {
System.out.print("请输入起点站名:");
start = cin.nextLine();
while (!IsT(name, start, n)) {
System.out.print("输入错误,请重新输入起点站名:");
start = cin.nextLine();
}
System.out.print("请输入终点站名:");
dest = cin.nextLine();
while (!IsT(name, dest, n)) {
System.out.print("输入错误,请重新输入终点站名:");
dest = cin.nextLine();
}
if (IsT(name, dest, n)) {
float distan = (float) Dijkstra(Ma, pa, n, Check(nam, start, n), Check(nam, dest, n));// 能查出此结点到其他点最短路径
int s, d;
s = Check(nam, start, n);
d = Check(nam, dest, n);
stanum = 0;
print(pa, nam, s, d);
System.out.println(stanum + " " + dest);
int fen = (int) (distan * 1000 / race / 60);
int miao = ((int) (distan * 1000 / race)) % 60;
int r;
if (distan > 6) {
if (distan > 12) {
if (distan > 32) {
r = (int) (6 + distan / 20);
} else
r = (int) (4 + distan / 10);
} else
r = 4;
} else
r = 3;
System.out.println("总运行距离为:" + distan + "km");
System.out.println("途径总站点数为:" + stanum);
System.out.println("运行时间" + fen + "分" + miao + "秒");
System.out.println("总票价为:" + r + "元");
} else {
System.out.println();
System.out.println("please choise your stations!");
}
}
return;
五、总结
本次个人项目在数据的存储方法上遇到了困难,参考了网上的一些代码和思想,采用了使用一维数组存储的特殊方法。本次个人作业提升了自己的编程思想和逻辑思维也熟悉了GitHub等的使用。