• 2017华为机试题Floyd算法


    小K是X区域的销售经理,他平常常驻“5”城市,并且经常要到“1”、“2”、“3”、“4”、“6”城市出差。当机场出现大雾情况时,会导致对应城市的所有航班的起飞及降落均停止(即不能从该城市出发,其他城市也不能到达该城市)。小K希望知道如果他需要到X城市出差时,如果遇到Y城市出现大雾,他最短的飞行时间及飞行路径。

    注意:当两个城市间不可达时,消耗时间默认取1000.

    各城市简的飞行时间如下表所示,加粗行代表始发城市,加粗列代表终点城市,矩阵中的值代表从始发城市飞到终点城市所耗时间(单位:小时),M代表不可达(注意飞行线程是单向的,即A->B不等于B->A),例如

    (1)从1号城市飞行到4号城市花费5h,从4号城市飞到1号城市花费2h

    (2)从5号城市飞行到3号城市不可达,从3号城市飞到5号城市花费7h

         1    2    3    4    5    6

    1  0h  2h  10h  5h  3h   M

    2  M   0h  12h   M   M   10h

    3  M   M    0h    M   7h  M

    4  2h  M    M     0h  2h  M

    5  4h  M    M     1h  0h  M

    6  3h  M    1h    M   2h  0h

    输入描述:

    输入出差城市X(X可为1、2、3、4、6)

    输入大雾城市(Y可为0、1、2、3、4、5、6,可为0时代表没有城市出现大雾)

    代码如下:

     1 import java.util.*;
     2 
     3 public class Main2 {
     4     private static int INF = 1000;
     5 
     6     private static Integer[][] dist;
     7     private static Integer[][] path;
     8 
     9     private static List<Integer> result = new ArrayList<Integer>();
    10 //调试
    11     public static void printMatrix(Integer[][] matrix) {
    12         for (int i = 0; i < matrix.length; i++) {
    13             for (int j = 0; j < matrix.length; j++)
    14                 System.out.print(matrix[i][j] + " ");
    15             System.out.println();
    16         }
    17     }
    18 //设置雾城市
    19     private static void setFog(int[][] matrix, int city) {
    20         for (int i = 0; i < matrix.length; i++) {
    21             matrix[i][city] = matrix[city][i] = INF;
    22         }
    23     }
    24 
    25     public static void main(String[] args) {
    26 
    27         int size = 6;
    28 
    29         int begin = 4;
    30         Scanner scan = new Scanner(System.in);
    31         int end = Integer.parseInt(scan.nextLine()) - 1;
    32         int foggy = Integer.parseInt(scan.nextLine()) - 1;
    33         scan.close();
    34 
    35         int[][] matrix = { { 0, 2, 10, 5, 3, INF },
    36                 { INF, 0, 12, INF, INF, 10 }, { INF, INF, 0, INF, 7, INF },
    37                 { 2, INF, INF, 0, 2, INF }, { 4, INF, INF, 1, 0, INF },
    38                 { 3, INF, 1, INF, 2, 0 } };
    39         init(size);
    40         //没有雾
    41         if (foggy != -1)
    42             setFog(matrix, foggy);
    43 //调用弗洛伊德
    44         floyd(matrix);
    45 
    46         findPath(begin, end);
    47         System.out.println(dist[begin][end]);
    48         for (int i = 0; i < result.size(); i++)
    49             result.set(i, result.get(i) + 1);
    50         if (dist[begin][end] == INF)
    51             result.removeAll(result);
    52         System.out.println(result);
    53     }
    54 //在path数组里找路径
    55     public static void findPath(int i, int j) {
    56         int ci = i, ccj = j;
    57         while (path[i][j] != -1) {
    58             int cj = path[i][j];
    59             result.add(cj);
    60             i = cj;
    61         }
    62         result.add(0, ci);
    63         result.add(ccj);
    64     }
    65 
    66     public static void floyd(int[][] matrix) {
    67         int size = matrix.length;
    68         for (int i = 0; i < size; i++)
    69             for (int j = 0; j < size; j++) {
    70                 path[i][j] = -1;
    71                 dist[i][j] = matrix[i][j];
    72             }
    73         for (int k = 0; k < size; k++) {
    74             for (int i = 0; i < size; i++) {
    75                 for (int j = 0; j < size; j++) {
    76                     if (dist[i][k] != INF && dist[k][j] != INF
    77                             && dist[i][k] + dist[k][j] < dist[i][j]) {
    78                         dist[i][j] = dist[i][k] + dist[k][j];
    79                         path[i][j] = k;
    80                     }
    81                 }
    82             }
    83         }
    84     }
    85 //初始化两个数组
    86     public static void init(int size) {
    87         path = new Integer[size][size];
    88         dist = new Integer[size][size];
    89     }
    90 }
  • 相关阅读:
    链表栈
    双链表
    Linux sar命令查看系统资源使用情况
    Linux 命令(w/who/whoami/who am i)
    shell脚本引用expect
    expect 免交互配置互信
    expect 远程传输文件
    expect 远程执行命令-02
    expect 远程执行命令-01
    expect 脚本自定义变量和位置变量
  • 原文地址:https://www.cnblogs.com/Jocelyn66/p/6687676.html
Copyright © 2020-2023  润新知