• Floyd弗洛伊德算法


    先看懂如何使用

    • 用Java实现一个地铁票价计算程序

    • String station = "A1 A2 A3 A4 A5 A6 A7 A8 A9 T1 A10 A11 A12 A13 T2 A14 A15 A16 A17 A18 B1 B2 B3 B4 B5 T1 B6 B7 B8 B9 B10 T2 B11 B12 B13 B14 B15";

    思路:step1: 设计为A1-A18, T1,T2,B1-B15个点

      step2:35个点做为arr[35][35],将相邻的点A1-A2 ...赋值为1, T1和左右两点复制为1,

                   自己_自己复制为0,

                   不相邻值为1000(代表无穷大),A18_B1复制为1000

      step 3

    //Floyd算法求解
    for(int k=0; k<length; k++){
    for(int i=0; i<length; i++) {
    for(int j=0; j<length; j++) {
    if((arr[i][k] + arr[k][j]) < arr[i][j]) {
    arr[i][j] = arr[i][k] + arr[k][j];
    }
    }
    }
    }
    package com.train;
    
    import java.lang.reflect.Array;
    import java.util.HashMap;
    import java.util.Scanner;
    import java.util.concurrent.ForkJoinPool;
    
    public class TrainDemo {
        public static void main(String[] args) {
    
    
            String station = "A1 A2 A3 A4 A5 A6 A7 A8 A9 T1 A10 A11 A12 A13 T2 A14 A15 A16 A17 A18 B1 B2 B3 B4 B5 B6 B7 B8 B9 B10 B11 B12 B13 B14 B15";
            String[] stationarr = station.split("\s{1,}"); //length=37
            HashMap<String, Integer> uniMap = new HashMap<>();
            for (int i = 0; i < stationarr.length; i++) {
    
                uniMap.put(stationarr[i], i);
            }
            //distanceMap.put("T1")
            System.out.println(uniMap.toString());
            //创造一个2维数组,twoarr
            // 不能访问的值为@@无穷大
            //自己和自己直接为0;
            //
    
            Integer[][] twoarr = new Integer[stationarr.length][stationarr.length];
            for (int i = 0; i < stationarr.length; i++) {
                for (int j = 0; j < stationarr.length; j++) {
                    if (i == j) {
                        twoarr[i][j] = 0;
                    } else {
                        twoarr[i][j] = 1000;
                    }
                }
            }
    
            for (int i = 0; i <stationarr.length-1 ; i++) {
                twoarr[i][i+1]=1;
                twoarr[i+1][i]=1;
            }
            //将能能到达的两点间设置为具体值
    //
    
    
            twoarr[uniMap.get("A18").intValue()][uniMap.get("B1").intValue()]=1;
            twoarr[9][9]=0;
            twoarr[14][14]=0;
            twoarr[9][14]=0;
            twoarr[14][9]=0;
            //相通
            twoarr[9][24]=1;
            twoarr[24][9]=1;
            twoarr[9][25]=1;
            twoarr[25][9]=1;
            twoarr[14][29]=1;
            twoarr[29][14]=1;
            twoarr[14][30]=1;
            twoarr[30][14]=1;
            twoarr[uniMap.get("A18")][uniMap.get("B1")] = 1000;
            twoarr[uniMap.get("B1")][uniMap.get("A18")] = 1000;
            //t1=9 t2=14
    //arr[stationMap.get("A1").intValue()][stationMap.get("A18").intValue()] = 1;
            for (int k = 0; k < 35; k++) {
    
    
                for (int i = 0; i <stationarr.length ; i++) {
                    for (int j = 0; j <stationarr.length ; j++) {
    
                         if(twoarr[i][k]+twoarr[k][j]<twoarr[i][j]){
                             twoarr[i][j] = twoarr[i][k] + twoarr[k][j];
                         }
                    }
                }
            }
            for (int i = 0; i < 35; i++) {
                for (int j = 0; j <35 ; j++) {
                    System.out.print(twoarr[i][j]+" ");
                }
                System.out.println();
    
            }
            while (true){ String s = new Scanner(System.in).nextLine();
            String[] sarr= s.split("\s{1,}");
            System.out.println(twoarr[uniMap.get(sarr[0])][uniMap.get(sarr[1])]);
            }
        }
    }

    atzhang

  • 相关阅读:
    原生JS之温故而知新(一)
    jQuery版本问题
    AngularJs我的学习整理(不定时修改)
    Js事件处理程序跨浏览器
    AngularJs的关于路由问题
    很棒的十句话,分享给大家。
    一个人为什么要努力?
    春熙路。
    成都
    springboot 使用mybatis-generator自动生成代码
  • 原文地址:https://www.cnblogs.com/zytcomeon/p/12993211.html
Copyright © 2020-2023  润新知