找回密码
 立即注册
快捷导航
发帖
设为首页收藏本站

推特萝莉福利姬-麻辣兔头【78P1V30MB】 飞猫

[複製鏈接]

13006082457VIP6 超级会员

發表於 2025-4-8 23:54:31 | 顯示全部樓層
66666666666666666

不如不见面 云起雪飞

發表於 2025-4-9 03:15:39 | 顯示全部樓層
222222222222222

1063633193a 云起雪飞

發表於 2025-4-9 17:24:48 | 顯示全部樓層
6666666666666666666666

qqxfVIP2 水晶会员

發表於 2025-4-9 23:40:09 來自手機 | 顯示全部樓層
66666666666

Thelostone 云起雪飞

發表於 2025-4-9 23:43:08 | 顯示全部樓層
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = Integer.parseInt(in.nextLine());
        String[] arr = in.nextLine().split(" ");
        int start = arr[0].charAt(0) - 'a';
        int end = arr[1].charAt(0) - 'a';
        String[] arr2 = new String[100];
        int i = 0;
        while (true) {
            String str = in.nextLine();
            if (str.equals("0000")) {
                break;
            }
            arr2[i++] = str;
        }
        int[][] matrix = new int[n][n];
        for (int j = 0; j < n; j++) {
            for (int k = 0; k < n; k++) {
                matrix[j][k] = Integer.MAX_VALUE;
            }
        }
        for (int j = 0; j < n; j++) {
            matrix[j][j] = 0;
        }
        for (int j = 0; j < i; j++) {
            String str = arr2[j];
            int a = str.charAt(0) - 'a';
            int b = str.charAt(2) - 'a';
            int c = Integer.parseInt(str.substring(4,5));
            matrix[a][b] = Math.min(matrix[a][b], c);
            matrix[b][a] = Math.min(matrix[b][a], c);
        }

        // Floyd-Warshall算法计算最短路径
        int[][] prev = new int[n][n];
        for (int j = 0; j < n; j++) {
            for (int k = 0; k < n; k++) {
                if (matrix[j][k] != Integer.MAX_VALUE && j != k) {
                    prev[j][k] = j;
                } else {
                    prev[j][k] = -1;
                }
            }
        }

        for (int j = 0; j < n; j++) {
            for (int k = 0; k < n; k++) {
                for (int l = 0; l < n; l++) {
                    if (matrix[k][j] != Integer.MAX_VALUE && matrix[j][l] != Integer.MAX_VALUE) {
                        if (matrix[k][l] > matrix[k][j] + matrix[j][l]) {
                            matrix[k][l] = matrix[k][j] + matrix[j][l];
                            prev[k][l] = prev[j][l];
                        }
                    }
                }
            }
        }

        // 如果没有路径
        if (matrix[start][end] == Integer.MAX_VALUE) {
            System.out.println("No Path");
            return;
        }

        // 回溯路径
        List<Character> path = new ArrayList<>();
        int current = end;
        while (current != start) {
            path.add((char) (current + 'a'));
            current = prev[start][current];
            if (current == -1) {
                System.out.println("No Path");
                return;
            }
        }
        path.add((char) (start + 'a'));
        Collections.reverse(path);

        // 输出路径
        StringBuilder sb = new StringBuilder();
        for (int j = 0; j < path.size(); j++) {
            sb.append(path.get(j));
            if (j != path.size() - 1) {
                sb.append(" ");
            }
        }
        System.out.print(sb.toString().trim());
        System.out.println();
    }
}

2982401509 云起雪飞

發表於 2025-4-10 09:06:42 | 顯示全部樓層
6666666666666666666

lock8899 云起雪飞

發表於 2025-4-10 20:52:35 | 顯示全部樓層
推特萝莉福利姬-麻辣兔头【78P1V30M

ooppppqq 云起雪飞

發表於 2025-4-11 02:49:05 | 顯示全部樓層
千里黄云白日曛,北风吹雁雪纷纷。——高适《别董大二首》

HDF179 云起雪飞

發表於 2025-4-11 23:41:57 | 顯示全部樓層
6666666666

脸小生 云起雪飞

發表於 5 天前 | 顯示全部樓層
谢谢分享
您需要登錄後才可以回帖 登录 | 立即注册

本版積分規則

下载客户端
安卓客户端 IOS客户端

快来加入我们吧!!

官方微信公众号

次元公馆

Powered by 飞雪论坛 X3.4© 2022 飞雪娱乐网 Inc.

手机版| 小黑屋| 飞雪论坛

GMT+8, 2025-4-19 10:40 , Processed in 0.122150 second(s), 17 queries , Redis On.