最短路

题目描述
本题为填空题,只需要算出结果后,在代码中使用输出语句将所填结果输出即可。

如下图所示,G 是一个无向图,其中蓝色边的长度是 1、橘色边的长度是 2、绿色边的长度是 3。

图片描述
image

则从 AA 到 SS 的最短距离是多少?

运行限制
最大运行时间:1s
最大运行内存: 128M

点击查看代码
import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        //在此输入您的代码...
        System.out.println("6");
        scan.close();
    }
}
点击查看代码
import java.util.*;

public class Main {
  private static final Scanner in = new Scanner(System.in);
  private static List<int[]> edges = new ArrayList<>();

// 好吧,不开玩笑了,应该这样写
  public static void main(String[] args) {
    add('A', 'C', 1);
    add('A', 'D', 1);
    add('A', 'E', 1);
    add('D', 'E', 1);
    add('E', 'I', 1);
    add('D', 'H', 1);
    add('H', 'I', 1);
    add('B', 'G', 1);
    add('F', 'G', 1);
    add('F', 'J', 1);
    add('K', 'N', 1);
    add('L', 'M', 1);
    add('N', 'P', 1);
    add('P', 'O', 1);
    add('O', 'Q', 1);
    add('Q', 'M', 1);
    add('L', 'R', 1);
    add('S', 'R', 1);
    add('M', 'S', 1);

    add('A', 'B', 2);
    add('B', 'J', 2);
    add('D', 'I', 2);
    add('D', 'G', 2);
    add('G', 'K', 2);
    add('K', 'P', 2);
    add('J', 'S', 2);
    add('M', 'N', 2);
    add('H', 'L', 2);

    add('E', 'I', 3);
    add('I', 'M', 3);
    add('G', 'I', 3);
    add('C', 'D', 3);
    add('C', 'G', 3);
    add('C', 'F', 3);
    add('O', 'R', 3);
    add('K', 'L', 3);

    int n = edges.size();

    int[] dist = new int[128];
    Arrays.fill(dist, Integer.MAX_VALUE >> 1);

    dist['A'] = 0;

    for (int i = 0; i < n - 1; i++) {
      for (int[] edge : edges) {
        int u = edge[0], v = edge[1], w = edge[2];
        dist[v] = Math.min(dist[v], dist[u] + w);
      }
    }

    System.out.println(dist['S']);
  }

  private static void add(char u, char v, int w) {
    edges.add(new int[] {u, v, w});
    edges.add(new int[] {v, u, w});
  }
}
posted @ 2022-04-25 17:57  wjxuriel  阅读(51)  评论(0)    收藏  举报