Subway travel route planning summary

github Address: https://github.com/anarkhwqh/Subway


data processing

The railway line across the road name and the site name into txt file, if this is a circular route, the starting and terminal station of the same station, the format is as follows:

1号线 苹果园 古城 八角游乐园········四惠 四惠东
2号线 西直门 积水潭 鼓楼大街········车公庄 西直门
·
·
·
S1线  石厂 小园 栗园庄············ 四道桥 金安桥

Read the file function

public void loadSubwayFile(String subway_file) {
    BufferedReader reader = null;
    File subway = new File(subway_file);
    try {
        reader = new BufferedReader(new FileReader(subway));
        String t = null;
        while ((t = reader.readLine()) != null) {
            List<String> line = Arrays.asList(t.split(" "));
            station.add(line);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Each row of a subway line, each row into an array and then saved as List, stored into the List<List<String>>station by Map<String, Integer> name_numnumber each subway station

public void mapping() {
    int cnt = 1;
    for (List<String> line : station) {
        //因为下标为0的都为线路名称,所以i从1开始
        for (int i = 1; i < line.size(); i++) {
            if (!name_num.containsKey(line.get(i))) {
                num_name.put(cnt, line.get(i));
                name_num.put(line.get(i), cnt++);
            }
        }
    }
}

According cnt number of shows, a total of 332 subway stations

Dijkstra Directions

Dijkstra

private static int[][] c = new int[500][500];
//存储地铁站连通情况,c[i][j]等于1表示两站相邻,等于0表示不相邻
private static int[] prev = new int[500];
//存储最短路径
private static int[] dist = new int[500];
//存储最短距离
private static boolean[] s = new boolean[500];
//记录某点是否遍历过
private static int n = 332;
//总站点数

void Dijkstra(int v) {
    Arrays.fill(s, false);
    Arrays.fill(prev, 0);
    for (int i = 1; i <= n; i++) {
        dist[i] = c[v][i];
        if (dist[i] != 999999)
            prev[i] = v;
    }
    dist[v] = 0;
    s[v] = true;

    for (int i = 2; i <= n; i++) {
        int tmp = 999999;
        int u = v;
        for (int j = 1; j <= n; j++) {
            if ((!s[j]) && dist[j] < tmp) {
                u = j;
                tmp = dist[j];
            }
        }

        s[u] = true;

        for (int j = 1; j <= n; j++) {
            if (dist[u] + c[u][j] < dist[j]) {
                dist[j] = dist[u] + c[u][j];
                prev[j] = u;
            } else if (dist[u] + c[u][j] == dist[j]) {
                int a = searchPath(v, u);
                int b = searchPath(v, prev[j]);
                if (u != j && a < b) {
                    if (a < b)
                        prev[j] = u;
                    else {
                        for (List<String> line : station) {
                            if (line.contains(num_name.get(u)) && line.contains(num_name.get(j)))
                                prev[j] = u;
                        }
                    }
                }
            }
        }
    }
}

In the above mapping()function to c[][]initialize

for (int i = 1; i < line.size(); i++) {
    if (i != 1) {
        int a = name_num.get(line.get(i));
        int b = name_num.get(line.get(i - 1));
        c[a][b] = c[b][a] = 1;
    }
}

searchPath

int searchPath(int v, int u) {
    int[] que = new int[500];
    int cnt = 0;//换乘次数
    int tot = 1;
    que[tot] = u;
    tot++;
    int tmp = prev[u];
    while (tmp != v) {
        que[tot] = tmp;
        tot++;
        tmp = prev[tmp];
    }
    que[tot] = v;
    for (int i = tot; i >= 1; i--) {
        boolean f = false;
        if (i == tot)
            f = true;
        else if (i != 1) {
            for (List<String> line : station) {
                if (line.contains(num_name.get(que[i + 1])) && line.contains(num_name.get(que[i - 1]))) {
                    f = true;
                    break;
                }
            }
        }
        if (i != 1 && !f) {
            cnt++;
        }
    }
    return cnt;
}

UI interface

No form of pages or the app, directly written in java the UI, relatively simple interface as

Sample Test

  • A longer route

  • Overlapping routes

  • Enter the starting point or end point error

  • Start and end are the same

# Deficiencies and concluded that at
the subway map is a sparse graph, and Dijkstra algorithm is more suitable for dense graph, but due to less Metro points, so the speed is faster. Followed by the visualization itself want to try definitive answers similar to the page in the subway station at the time of purchase, a click on an area, then the piece of area enlargement, in select metro station, you can drag the map, but not implemented . There has not been a problem to solve, when writing UI interface in Java, put a picture adjusted to the right size is always distortion is very serious, I do not know how to solve. Finally, for the individual assignments, inefficient, bureaucratic very beginning, very often the bug are some of the details, thinking in the process of writing code is not comprehensive, leading to take a long time to adjust back bug.

Guess you like

Origin www.cnblogs.com/anarkh/p/11672050.html