Java图结构-模拟校园地图-迪杰斯特拉(Dijkstra)算法求最短路径 #谭子


一、前言

        这段时间也是忙得挺充实的,基本上每天不是在整活就是在整活的路上,差点忘记在C站还有号了。等放假后吧,会多多活跃的。

        这周是数据结构的实训周,上面课题给了五个选项:图的应用—校园地图、排序算法的实现、栈的应用—迷宫问题、哈夫曼编译码器、图书管理系统,因为有四个选项感觉蛮多人选的,而且之前的作业里也有类似参考的源码,我们组就选了比较无人问津的图应用

        打算整一个模拟校区的程序。大致内容就是构造图结构,运用迪杰斯特拉算法来求得图中两点的最短路径。在讨论设计图结构时,因为懒得设计,干脆直接把整个校区的地点都标出来......

57cde3dc9e9e46b3863635ca6301a500.png


二、模拟校园地图描述

【基本要求】

1.地图所含地点不少于15个。以图中顶点表示校内各地点,存放地点名称、代号、简介等信息;以边表示路径,存放路径长度等相关信息。

2. 设计地图的逻辑结构和物理结构。

3. 使用Java语言编写程序,为来访客人提供图中任意地点相关信息的查询,以及为来访客人提供图中任意地点的问路查询,即查询任意两个地点之间的一条最短的简单路径。

【测试数据】

  1. 输入学院各地点名及有关该地点的基本信息;
  2. 输入相邻两地点以及它们之间的路径长度;
  3. 查询学院的任一地点的基本信息;
  4. 查询学院任两个地点间的最短路径及路径长度。

【实现提示】

        一般情况下,校园的道路是双向通行的,可设校园平面图是一个无向网。顶点和边均含有相关信息。


三、分析题目及相关绘图

1.题目分析

        逻辑结构上选择非线性逻辑结构中的图结构,存储结构上选择了顺序存储结构,在界面菜单提示下进入到学校简介及导航系统,导航系统中设立 查询所有地点、 查询单一地点信息介绍、 查询两地点之间最短距离 三个功能。其中,最短路径需要使用到迪杰斯特拉算法。大致内容很好理解,就是对图的概念和所学知识的综合起来应用。

        我们打算设立29个景点作为图元素,通过结点类去赋予单一元素与其之间有直接连接的其他元素之间的距离。(这句话有点绕,就是一个元素与其无向边的另外一个元素间的权重,是我们自己设立的。)

        OK,接下来就是关于这次实训的一些绘图展示。

1.相关绘图

1934a442981646e6b76f614c31c1a74b.png


 图1. 系统分支图

e042a0dc3cf94735976e67e48648ce1b.png

图2.  类说明图

(具体代码可在下文获取)

50ba0087b3304450ac00d5ed585e69e9.png

 图3. 程序流程图

     在main方法中使用Attrachtion类构建29个元素对象,用来存储29个景点信息。AttractionGraph结点类是继承ArrayList集合的,使用AttractionGraph结点类构造graph,将graph作为一维数组存储29个Attrachtion构造出的元素。并对每一个graph的元素之间的距离进行填充数据,所有构建好的graph内容如 图4. 图元素的构建 所示。

       构造出一个无限长度的ArrayList集合表作为空的邻接表,将所有构建好的graph放入ArrayList集合存储起来,图结构如  图5. 图结构  所示。通过迪杰斯特拉算法求得两景点的最短路径。此算法作为查询任意两景点之间最短路径的实现方法,首先确定输入的元素是否存在,在判断为存在的前提后,将开始遍历所有元素及其路径权值,获取到出发景点和目的景点的编号,因为出发顶点本身不需要再求最短路径,故只需要遍历图长度-1。第一次内循环,找出最小的最短路径,并记录最小的最短路径对应的顶点编号和路径长度。创建两个辅助集合:dish和path,对辅助集合dist和path进行修改,查找出新的最短路径集合。最终通过对辅助数组的查找,将最短路径返回到图起始点,但由于最终结构是倒序查找,最后输出的集合顺序需要进行反转。

ed3e010bfe444df18be480ac13b40ca1.png

 图4. 图元素的构建

(通过顺序表来构建单个图元素,最后将所有元素放入ArrayList集合来合成邻接表)

cdd449b40c7245cd99eb9c29815c70c6.png

 图5. 图结构

       ( 注:这绘图,十分滴珍贵,都是啊宇哥画的 ,工匠精神了属于是)


四、代码部分

1.GraphNode类

package MySchool;
//有向边对应的顶点编号和有向边的长度
public class GraphNode{
    private int no;
    private int distance;
    private GraphNode next;

    public GraphNode(int no, int distance,GraphNode next) {
        this.no = no;
        this.distance = distance;
        this.next = next;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public int getDistance() {
        return distance;
    }

    public void setDistance(int distance) {
        this.distance = distance;
    }

    public GraphNode getNext() {
        return next;
    }

    public void setNext(GraphNode next) {
        this.next = next;
    }

    public GraphNode add(GraphNode graph) {
        GraphNode temp = this;
        while (temp.next != null) {
            temp = temp.next;
        }
        temp.next = graph;
        return graph;
    }

    public String toString() {
        return "GraphNode{" +
                "no=" + no +
                ", distance=" + distance +
                ", next=" + next +
                '}';
    }
}

2.Menu类(管理文字)

package MySchool;
public class Menu {
    public static void MainMenu(){
        System.out.println("==============================");
        System.out.println("------------主菜单-------------");
        System.out.println("----------1.进入导航系统--------");
        System.out.println("----------2.学校简介-----------");
        System.out.println("----------0.退出程序-----------");
        System.out.println("==============================");
    }//主菜单界面
    public static void Navigation(){
        System.out.println("==============================");
        System.out.println("====欢迎使用广交院校园导航系统=====");
        System.out.println("请选择操作:\nA.显示学校所有景点\nB.查询学校景点位置信息\nC.查询任意两景点之间最短路径\n0.返回主界面");
        System.out.println("==============================");
    }//导航系统菜单
    public static void SchoolProfile(){
        System.out.println("*****************************************");
        System.out.println("*  广东交通职业技术学院于1999年经教育部批准,由 *\n" +
                "* 创建于1959年的广东交通学校、创建于1960年的广 *\n" +
                "* 东省航运学校合并组建成立2008年7月,经广东省人 *\n" +
                "* 民政府批准同意,经由广东省交通运输厅移交广东省 *\n" +
                "* 教育厅管理。截至2020年4月,学校拥有广州天河、 *\n" +
                "* 花都、清远三个校区,占地面积1000余亩;下设有9 *\n" +
                "* 个二级学院,开设高职招生专业44个,全日制在校生 *\n" +
                "* 15500余人。                            *");
        System.out.println("*****************************************");
    }//学校简介信息
    public static void back(){
        System.out.println("已自动回到主菜单!");
        DebugSchool origin = new DebugSchool();
        origin.main(new String[]{});
    }//直接返回主菜单的操作
}

3.Attraction类

package MySchool;
public class Attraction{
    private int no;
    private String name;
    private String introduce;
    private String position;
    private double longitude;
    private double latitude;
    private GraphNode next;

    public Attraction(int no, String name, String introduce, String position, double longitude, double latitude, GraphNode next) {
        this.no = no;
        this.name = name;
        this.introduce = introduce;
        this.position = position;
        this.longitude = longitude;
        this.latitude = latitude;
        this.next = next;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getIntroduce() {
        return introduce;
    }

    public void setIntroduce(String introduce) {
        this.introduce = introduce;
    }

    public String getPosition() {
        return position;
    }

    public void setPosition(String position) {
        this.position = position;
    }

    public double getLongitude() {
        return longitude;
    }

    public void setLongitude(double longitude) {
        this.longitude = longitude;
    }

    public double getLatitude() {
        return latitude;
    }

    public void setLatitude(double latitude) {
        this.latitude = latitude;
    }

    public GraphNode getNext() {
        return next;
    }

    public void setNext(GraphNode next) {
        this.next = next;
    }

    public String toString() {
        return "地点简介{" +
                "景点代号:" + no +
                ", 名称:'" + name + '\'' +
                ", 景点介绍:'" + introduce + '\'' +
                ", 地址:'" + position + '\'' +
                ", 经度:" + longitude +
                ", 纬度:" + latitude +
                '}';
    }
}

4.AttractionGraph类(算法)

package MySchool;
import java.util.*;
class AttractionGraph extends ArrayList<Attraction> {
    // 通过景点名称查询景点是否存在
    public boolean containsAttraction(String key){
        List<String> attractionNames = new ArrayList<>();
        for (Attraction attraction : this){
            attractionNames.add(attraction.getName());
        }
        return attractionNames.contains(key);
    }

    /**
     * 通过出发景点和目的景点查询最短路径 ,迪杰斯特拉(Dijkstra)算法
     * startAttraction 出发景点
     * stopAttraction 目的景点
     */
    public Map<Attraction,Integer> shortestPath(String startAttraction, String stopAttraction){
        //出发景点的编号
        int startNo = 0;
        //目的景点的编号
        int stopNo = 0;
        //通过出发景点和目的景点的名称查找他们各自的编号
        for (Attraction attraction : this) {
            if (attraction.getName().equals(startAttraction)) {
                startNo = attraction.getNo();
            }
            if (attraction.getName().equals(stopAttraction)) {
                stopNo = attraction.getNo();
            }
        }
        //辅助集合,存放到达i的最短路径的长度
        ArrayList<Integer> dist = new ArrayList<>();
        //辅助集合,存放到达i的最短路径的前驱节点
        ArrayList<Integer> path = new ArrayList<>();
        //辅助集合,标记已经找到最短路径的节点
        ArrayList<Integer> s = new ArrayList<>();
        //构造初始dis、path、s集合
        /************ 构造初始dis、path、s集合-start **************/
        //dist中元素默认值为无穷大,表示无法到达
        //path中元素默认值为-1,表示无法到达或者出发节点本身
        //s中元素默认值为0,表示还未找到最短路径
        for(Attraction attraction : this){
            dist.add(Integer.MAX_VALUE);
            path.add(-1);
            s.add(0);
        }
        //出发节点的dist为0,表示不需要行动就可到达
        dist.set(startNo,0);
        //从出发节点到他自身不需要再去寻找最短路径,就是他自己,故直接设为1,表示已经找到
        s.set(startNo,1);
        //辅助指针,用来遍历出发景点的可直接达到的相邻节点
        GraphNode temp = this.get(startNo).getNext();
        //遍历出发顶点的有向边
        while (temp != null) {
            //修改可到达的顶点的dist值
            dist.set(temp.getNo(), temp.getDistance());
            //修改可到达的顶点的path值,即前驱顶点
            path.set(temp.getNo(), startNo);
            //继续往后遍历
            temp = temp.getNext();
        }
        /************ 构造初始dis、path、s集合-stop **************/
        /************    最短路径的求解-start       **************/
        //因为出发顶点本身不需要再求最短路径,故只需要遍历this.size()-1次
        for (int i = 0; i < this.size()-1; i++) {
            int mix = Integer.MAX_VALUE;
            int mixIndex = startNo;
            //第一次内循环,找出最小的最短路径,并记录最小的最短路径对应的顶点编号和路径长度
            for (int j=0;j<dist.size();j++){
                if (s.get(j) == 0 && dist.get(j) < mix){
                    mixIndex = j;
                    mix = dist.get(j);
                }
            }
            s.set(mixIndex,1);
            GraphNode temp2 = this.get(mixIndex).getNext();
            //对辅助集合dist和path进行修改,查找出新的最短路径集合
            while (temp2 != null) {
                if (s.get(temp2.getNo()) == 0 && mix + temp2.getDistance() < dist.get(temp2.getNo())){
                    dist.set(temp2.getNo(),mix + temp2.getDistance());
                    path.set(temp2.getNo(),mixIndex);
                }
                temp2 = temp2.getNext();
            }
        }
        /************     最短路径的求解-stop       **************/
        /************     通过对辅助数组的查找,将最短路径返回-start       **************/
        Map<Attraction,Integer> map = new LinkedHashMap<>();
        AttractionGraph attractionGraph = new AttractionGraph();
        //查找路径,并返回
        map.put(this.get(startNo),0);
        while (stopNo != startNo){
            if (path.get(stopNo) == -1){
                return null;
            }
            attractionGraph.add(this.get(stopNo));
            stopNo = path.get(stopNo);
        }
        //因为是倒序查找的,所以找完之后要将集合的顺序反转
        Collections.reverse(attractionGraph);
        attractionGraph.forEach(attraction -> map.put(attraction,dist.get(attraction.getNo())));
        /************     通过对辅助数组的查找,将最短路径返回-stop       **************/
        return map;
    }

    //通过顶点名称查找顶点的详细信息
    public Attraction searchAttraction(String key){
        for (Attraction attraction : this) {
            if (attraction.getName().equals(key)) {
                return attraction;
            }
        }
        return null;
    }
}

5.DebugSchool类(运行)

package MySchool;
import java.util.*;
import java.util.Scanner;
public class DebugSchool {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Menu.MainMenu();
        //无向图的构建(29个元素),构造邻接表
        Attraction attraction0 = new Attraction(0,"校门口","校门口","清城区东城街道中宿路34号",113.107876,23.748527,null);
        Attraction attraction1 = new Attraction(1,"求是楼","教学楼,有自动贩卖机","清城区东城街道中宿路34号",113.108316,23.748974,null);
        Attraction attraction2 = new Attraction(2,"笃实楼","教学楼,有自动贩卖机","清城区东城街道中宿路34号",113.107346,23.748883,null);
        Attraction attraction3 = new Attraction(3,"聚贤楼","学校办公的地方","清城区东城街道中宿路34号",113.106834,23.749056,null);
        Attraction attraction4 = new Attraction(4,"荟贤楼","学校办公的地方","清城区东城街道中宿路34号",113.106259,23.749056,null);
        Attraction attraction5 = new Attraction(5,"济贤楼","学校办公的地方","清城区东城街道中宿路34号",113.10563,23.749003,null);
        Attraction attraction6 = new Attraction(6,"创新楼","可以拿快递,教学楼","清城区东城街道中宿路34号",113.108482,23.750665,null);
        Attraction attraction7 = new Attraction(7,"立业楼","教学楼,有自动贩卖机,有电梯","清城区东城街道中宿路34号",113.108033,23.750781,null);
        Attraction attraction8 = new Attraction(8,"思齐苑","教师宿舍楼","清城区东城街道中宿路34号",113.107849,23.750723,null);
        Attraction attraction9 = new Attraction(9,"明德楼","教学楼,有电梯","清城区东城街道中宿路34号",113.108316,23.750938,null);
        Attraction attraction10 = new Attraction(10,"厚德楼","教学楼,有自动贩卖机","清城区东城街道中宿路34号",113.108316,23.750568,null);
        Attraction attraction11 = new Attraction(11,"三江园","学生饭堂,有百货商店","清城区东城街道中宿路34号",113.107224,23.750818,null);
        Attraction attraction12 = new Attraction(12,"融创楼",".融创楼","清城区东城街道中宿路34号",113.108509,23.750892,null);
        Attraction attraction13 = new Attraction(13,"融新楼","教学楼,可以上电脑课,可以学习维修汽车","清城区东城街道中宿路34号",113.108477,23.751004,null);
        Attraction attraction14 = new Attraction(14,"图书馆","师生安静学习工作的场所","清城区东城街道中宿路34号",113.108477,23.751678,null);
        Attraction attraction15 = new Attraction(15,"体育馆","室内有羽毛球场,乒乓球场,篮球场,排球场","清城区东城街道中宿路34号",113.108477,23.751456,null);
        Attraction attraction16 = new Attraction(16,"箐华楼","有百货商店,各学院办公的地方","清城区东城街道中宿路34号",113.108262,23.751322,null);
        Attraction attraction17 = new Attraction(17,"沁香园","学生饭堂,有百货商店","清城区东城街道中宿路34号",113.108262,23.756574,null);
        Attraction attraction18 = new Attraction(18,"运动场","有400米跑道。篮球场,足球场,排球场","清城区东城街道中宿路34号",113.108545,23.751504,null);
        Attraction attraction19 = new Attraction(19,"勤济楼","学校放清洁物品,交宿舍费","清城区东城街道中宿路34号",113.108545,23.751567,null);
        Attraction attraction20 = new Attraction(20,"清雅苑","学生宿舍楼,有自动贩卖机,女生宿舍楼","清城区东城街道中宿路34号",113.107759,23.751347,null);
        Attraction attraction21 = new Attraction(21,"清新苑","学生宿舍楼,男生宿舍楼,有自动贩卖机,校医室,理发店","清城区东城街道中宿路34号",113.107759,23.751128,null);
        Attraction attraction22 = new Attraction(22,"众创空间","发明小物件","清城区东城街道中宿路34号",113.107759,23.758754,null);
        Attraction attraction23 = new Attraction(23,"清逸苑","学生宿舍楼,女生宿舍楼,有自动贩卖机","清城区东城街道中宿路34号",113.107759,23.758743,null);
        Attraction attraction24 = new Attraction(24,"清致苑","学生宿舍楼,男生宿舍楼,有自动贩卖机","清城区东城街道中宿路34号",113.10567,23.75439,null);
        Attraction attraction25 = new Attraction(25,"清风苑","学生宿舍楼,男女混合,有自动贩卖机","清城区东城街道中宿路34号",113.105064,23.753877,null);
        Attraction attraction26 = new Attraction(26,"清和苑","学生宿舍楼,男生宿舍楼,有自动贩卖机","清城区东城街道中宿路34号",113.105064,23.753267,null);
        Attraction attraction27 = new Attraction(27,"北门","不可以出校门","清城区东城街道中宿路34号",113.107072,23.754804,null);
        Attraction attraction28 = new Attraction(28,"东门"," 不可以出校门","清城区东城街道中宿路34号",113.110584,23.751058,null);
        AttractionGraph graph = new AttractionGraph();
        graph.add(attraction0);graph.add(attraction1);graph.add(attraction2);
        graph.add(attraction3);graph.add(attraction4);graph.add(attraction5);
        graph.add(attraction6);graph.add(attraction7);graph.add(attraction8);
        graph.add(attraction9);graph.add(attraction10);graph.add(attraction11);
        graph.add(attraction12);graph.add(attraction13);graph.add(attraction14);
        graph.add(attraction15);graph.add(attraction16);graph.add(attraction17);
        graph.add(attraction18);graph.add(attraction19);graph.add(attraction20);
        graph.add(attraction21);graph.add(attraction22);graph.add(attraction23);
        graph.add(attraction24);graph.add(attraction25);graph.add(attraction26);
        graph.add(attraction27);graph.add(attraction28);
        //数据插入
        graph.get(0).setNext(new GraphNode(1,50,null));
        graph.get(0).getNext()
                .add(new GraphNode(2,50,null))
                .add(new GraphNode(6,100,null))
                .add(new GraphNode(7,100,null))
                .add(new GraphNode(9,120,null))
                .add(new GraphNode(14,150,null));
        graph.get(1).setNext(new GraphNode(0,50,null));
        graph.get(1).getNext()
                .add(new GraphNode(0,50,null))
                .add(new GraphNode(6,25,null));
        graph.get(2).setNext(new GraphNode(0,50,null));
        graph.get(2).getNext()
                .add(new GraphNode(0,50,null))
                .add(new GraphNode(3,15,null))
                .add(new GraphNode(7,25,null));
        graph.get(3).setNext(new GraphNode(0,65,null));
        graph.get(3).getNext()
                .add(new GraphNode(2,15,null))
                .add(new GraphNode(4,15,null))
                .add(new GraphNode(7,15,null))
                .add(new GraphNode(8,20,null));
        graph.get(4).setNext(new GraphNode(0,80,null));
        graph.get(4).getNext()
                .add(new GraphNode(3,15,null))
                .add(new GraphNode(5,15,null))
                .add(new GraphNode(8,22,null));
        graph.get(5).setNext(new GraphNode(0,95,null));
        graph.get(5).getNext()
                .add(new GraphNode(4,15,null))
                .add(new GraphNode(8,29,null));
        graph.get(6).setNext(new GraphNode(0,75,null));
        graph.get(6).getNext()
                .add(new GraphNode(0,100,null))
                .add(new GraphNode(1,25,null))
                .add(new GraphNode(9,25,null))
                .add(new GraphNode(12,20,null));
        graph.get(7).setNext(new GraphNode(0,75,null));
        graph.get(7).getNext()
                .add(new GraphNode(0,100,null))
                .add(new GraphNode(2,25,null))
                .add(new GraphNode(3,15,null))
                .add(new GraphNode(8,18,null))
                .add(new GraphNode(10,25,null));
        graph.get(8).setNext(new GraphNode(0,85,null));
        graph.get(8).getNext()
                .add(new GraphNode(3,20,null))
                .add(new GraphNode(4,22,null))
                .add(new GraphNode(5,29,null))
                .add(new GraphNode(7,18,null))
                .add(new GraphNode(10,19,null))
                .add(new GraphNode(11,35,null));
        graph.get(8).setNext(new GraphNode(0,93,null));
        graph.get(8).getNext()
                .add(new GraphNode(3,20,null))
                .add(new GraphNode(4,22,null))
                .add(new GraphNode(5,29,null))
                .add(new GraphNode(7,18,null))
                .add(new GraphNode(10,25,null))
                .add(new GraphNode(11,35,null));
        graph.get(9).setNext(new GraphNode(0,100,null));
        graph.get(9).getNext()
                .add(new GraphNode(0,120,null))
                .add(new GraphNode(6,25,null))
                .add(new GraphNode(12,35,null))
                .add(new GraphNode(13,40,null))
                .add(new GraphNode(14,50,null));
        graph.get(10).setNext(new GraphNode(0,100,null));
        graph.get(10).getNext()
                .add(new GraphNode(0,120,null))
                .add(new GraphNode(7,25,null))
                .add(new GraphNode(8,19,null))
                .add(new GraphNode(11,25,null))
                .add(new GraphNode(14,50,null));
        graph.get(11).setNext(new GraphNode(0,120,null));
        graph.get(11).getNext()
                .add(new GraphNode(8,35,null))
                .add(new GraphNode(10,25,null))
                .add(new GraphNode(16,210,null))
                .add(new GraphNode(19,225,null));
        graph.get(12).setNext(new GraphNode(0,95,null));
        graph.get(12).getNext()
                .add(new GraphNode(6,20,null))
                .add(new GraphNode(9,35,null))
                .add(new GraphNode(13,15,null));
        graph.get(13).setNext(new GraphNode(0,110,null));
        graph.get(12).getNext()
                .add(new GraphNode(9,40,null))
                .add(new GraphNode(12,15,null))
                .add(new GraphNode(15,35,null));
        graph.get(14).setNext(new GraphNode(0,150,null));
        graph.get(14).getNext()
                .add(new GraphNode(0,150,null))
                .add(new GraphNode(9,50,null))
                .add(new GraphNode(10,50,null))
                .add(new GraphNode(15,125,null))
                .add(new GraphNode(16,15,null));
        graph.get(15).setNext(new GraphNode(0,145,null));
        graph.get(15).getNext()
                .add(new GraphNode(13,35,null))
                .add(new GraphNode(14,125,null))
                .add(new GraphNode(16,100,null))
                .add(new GraphNode(17,110,null))
                .add(new GraphNode(28,13,null));
        graph.get(16).setNext(new GraphNode(0,165,null));
        graph.get(16).getNext()
                .add(new GraphNode(11,210,null))
                .add(new GraphNode(14,15,null))
                .add(new GraphNode(15,100,null))
                .add(new GraphNode(17,36,null))
                .add(new GraphNode(19,36,null));
        graph.get(17).setNext(new GraphNode(0,201,null));
        graph.get(17).getNext()
                .add(new GraphNode(16,36,null))
                .add(new GraphNode(15,110,null))
                .add(new GraphNode(18,42,null))
                .add(new GraphNode(19,26,null))
                .add(new GraphNode(20,52,null))
                .add(new GraphNode(28,124,null));
        graph.get(18).setNext(new GraphNode(0,243,null));
        graph.get(18).getNext()
                .add(new GraphNode(17,42,null))
                .add(new GraphNode(20,76,null))
                .add(new GraphNode(27,116,null))
                .add(new GraphNode(28,201,null));
        graph.get(19).setNext(new GraphNode(0,201,null));
        graph.get(19).getNext()
                .add(new GraphNode(11,225,null))
                .add(new GraphNode(16,36,null))
                .add(new GraphNode(17,26,null))
                .add(new GraphNode(20,23,null))
                .add(new GraphNode(21,105,null));
        graph.get(20).setNext(new GraphNode(0,224,null));
        graph.get(20).getNext()
                .add(new GraphNode(21,40,null))
                .add(new GraphNode(19,23,null))
                .add(new GraphNode(17,52,null))
                .add(new GraphNode(18,76,null))
                .add(new GraphNode(27,126,null));
        graph.get(21).setNext(new GraphNode(0,264,null));
        graph.get(21).getNext()
                .add(new GraphNode(22,15,null))
                .add(new GraphNode(20,40,null))
                .add(new GraphNode(23,20,null))
                .add(new GraphNode(19,105,null));
        graph.get(22).setNext(new GraphNode(0,279,null));
        graph.get(22).getNext()
                .add(new GraphNode(27,20,null))
                .add(new GraphNode(23,20,null))
                .add(new GraphNode(21,15,null));

        graph.get(23).setNext(new GraphNode(0,284,null));
        graph.get(23).getNext()
                .add(new GraphNode(21,20,null))
                .add(new GraphNode(22,20,null))
                .add(new GraphNode(24,20,null));
        graph.get(24).setNext(new GraphNode(0,304,null));
        graph.get(24).getNext()
                .add(new GraphNode(23,20,null))
                .add(new GraphNode(25,20,null));
        graph.get(25).setNext(new GraphNode(0,324,null));
        graph.get(25).getNext()
                .add(new GraphNode(24,20,null))
                .add(new GraphNode(26,20,null));
        graph.get(26).setNext(new GraphNode(0,344,null));
        graph.get(26).getNext()
                .add(new GraphNode(25,20,null));
        graph.get(27).setNext(new GraphNode(0,299,null));
        graph.get(27).getNext()
                .add(new GraphNode(18,116,null))
                .add(new GraphNode(20,126,null))
                .add(new GraphNode(22,20,null));
        graph.get(28).setNext(new GraphNode(0,158,null));
        graph.get(28).getNext()
                .add(new GraphNode(15,13,null))
                .add(new GraphNode(17,124,null))
                .add(new GraphNode(18,201,null));

        int co = sc.nextInt();
        switch (co){
            case 1:
                Menu.Navigation();
                char key = ' ';
                Scanner scanner = new Scanner(System.in);
                key = scanner.next().charAt(0);//获取用户输入的第一个字符
                boolean loop = true;
                while (loop==true){
                    switch (key){
                        case 'A': //查询所有景点信息
                            for (Attraction value : graph) {
                                System.out.print(value.getName() + "\t");
                            }
                            System.out.println();
                            Menu.back();
                            break;
                        case 'B':
                            System.out.println("请输入景点的名称:");
                            String attractionName = scanner.next();

                            Attraction attractionDetail = graph.searchAttraction(attractionName);
                            if (attractionDetail != null){
                                System.out.println(attractionDetail);
                                break;
                            }
                            System.out.println("该景点不存在!");
                        case 'C'://查询俩景点最短距离
                            System.out.println("请输入出发景点的名称:");
                            String startAttraction = scanner.next();
                            if (!graph.containsAttraction(startAttraction)){
                                System.out.println("该景点不存在!");
                                break;
                            }
                            System.out.println("请输入到达景点的名称:");
                            String stopAttraction = scanner.next();
                            if (!graph.containsAttraction(stopAttraction)){
                                System.out.println("该景点不存在!");
                                break;
                            }
                            Map<Attraction,Integer> attractions = graph.shortestPath(startAttraction,stopAttraction);
                            if (attractions == null){
                                System.out.println("路径不存在!");
                                break;
                            }
                            System.out.println("\n最短路径为:");
                            attractions.forEach((id,value) -> System.out.println("到达 "+id.getName()+",需"+value+"米-->"));
                            System.out.println();
                            Menu.back();
                            break;
                        case 0:     //退回主菜单
                            Menu.back();
                            break;
                        default:
                            Menu.back();
                            break;
                    }break;}
                break;
            case 2:     //学校简介内容
                Menu.SchoolProfile();
                System.out.printf("输入0返回到主菜单");
                int choice=sc.nextInt();
                if (choice==0){
                    Menu.back();
                }else
                    break;
            case 0:     //退出程序
                break;
            default:    //输入其他整数都返回到主菜单
                break;
        }
    }
}


五、运行截图

4419fc91bd404daf8d28a0bc0b46f312.png


六、总结

        这次实训让我能够更加深刻地了解到图结构及迪杰斯特拉算法,同时能将所学的知识运用到实用程序设计中,做出模拟校园地图的效果。在学习如何使用图结构的同时,我清楚地明白了数据结构对程序的影响是十分大的,不同的算法及数据结构能够更好地展现出不同的效果。

        最终在小组成员的努力下,我们完成了该课题,这也是团队合作的集中体现。组内分工挺合理的,我们组在编写代码时,能够做到效率最大化,集长补短,从无到有。面对着五彩斑斓的黑,绞尽脑汁去让代码成为程序,着实是非常烧脑了。

        不是所有的努力都能换来回报,有时候真的是很离谱暴躁的,但想了想,冷静点,不要钻牛角尖,很多操作就不至于变形了。

        最后的最后,再次感谢啊宇哥的工匠绘图精神,以及老叶老颜的神奇操作。

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值