代码随想录算法训练营第五十八天| 117. 软件构建、47. 参加科学大会

[KamaCoder] 117. 软件构建

[KamaCoder] 117. 软件构建 文章解释

题目描述

某个大型软件项目的构建系统拥有 N 个文件,文件编号从 0 到 N - 1,在这些文件中,某些文件依赖于其他文件的内容,这意味着如果文件 A 依赖于文件 B,则必须在处理文件 A 之前处理文件 B (0 <= A, B <= N - 1)。请编写一个算法,用于确定文件处理的顺序。

输入描述

第一行输入两个正整数 N, M。表示 N 个文件之间拥有 M 条依赖关系。

后续 M 行,每行两个正整数 S 和 T,表示 T 文件依赖于 S 文件。

输出描述

输出共一行,如果能处理成功,则输出文件顺序,用空格隔开。 

如果不能成功处理(相互依赖),则输出 -1。

输入示例
5 4
0 1
0 2
1 3
2 4
输出示例
0 1 2 3 4

文件处理的顺序除了示例中的顺序,还存在

0 2 4 1 3

0 2 1 3 4

等等合法的顺序。

数据范围:

0 <= N <= 10 ^ 5

1 <= M <= 10 ^ 9

每行末尾无空格。

[KamaCoder] 117. 软件构建

自己看到题目的第一想法

    无

看完代码随想录之后的想法

    找到所有入度为 0 的节点, 保存到列表中. 遍历所有入度为 0 的节点, 将该节点添加到结果集合中, 同时将该节点指向的节点的入度减 1.  如果该节点入度变为 0, 则也添加到入度为 0 的节点列表中.

import java.util.Scanner;
import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

public class Main {
    private static Map<Integer, List<Integer>> nodes = new HashMap<>();;
    private static int[] inDegress = null;
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int fileCount = 0;
        int fileDependency = 0;
        while (scanner.hasNext()) {
            fileCount = scanner.nextInt();
            fileDependency = scanner.nextInt();
            inDegress = new int[fileCount];
            int fromFile;
            int toFile;
            for (int i = 0; i < fileDependency; i++) {
                fromFile = scanner.nextInt();
                List<Integer> toFiles = nodes.get(fromFile);
                if (toFiles == null) {
                    toFiles = new ArrayList<>();
                    nodes.put(fromFile, toFiles);
                }
                toFile = scanner.nextInt();
                toFiles.add(toFile);
                inDegress[toFile]++;
            }
        }
        Deque<Integer> zeroDegressNodes = new LinkedList<>();
        List<Integer> result = new ArrayList<>();
        for (int i = 0; i < inDegress.length; i++) {
            if (inDegress[i] == 0) {
                zeroDegressNodes.add(i);
            }
        }
        while (!zeroDegressNodes.isEmpty()) {
            int node = zeroDegressNodes.pop();
            result.add(node);
            List<Integer> indegressNodes = nodes.get(node);
            if (indegressNodes == null) {
                continue;
            }
            for (int indegressNode : indegressNodes) {
                inDegress[indegressNode]--;
                if (inDegress[indegressNode] == 0) {
                    zeroDegressNodes.add(indegressNode);
                }
            }
        }
        if (result.size() != fileCount) {
            System.out.println(-1);
        } else {
            for (int i = 0; i < result.size() - 1; i++) {
                System.out.print(result.get(i) + " ");
            }
            System.out.println(result.get(fileCount - 1));
        }
    }
}

自己实现过程中遇到哪些困难

    无.

[KamaCoder] 47. 参加科学大会(第六期模拟笔试)

[KamaCoder] 47. 参加科学大会(第六期模拟笔试) 文章解释

题目描述

小明是一位科学家,他需要参加一场重要的国际科学大会,以展示自己的最新研究成果。

小明的起点是第一个车站,终点是最后一个车站。然而,途中的各个车站之间的道路状况、交通拥堵程度以及可能的自然因素(如天气变化)等不同,这些因素都会影响每条路径的通行时间。

小明希望能选择一条花费时间最少的路线,以确保他能够尽快到达目的地。

输入描述

第一行包含两个正整数,第一个正整数 N 表示一共有 N 个公共汽车站,第二个正整数 M 表示有 M 条公路。 

接下来为 M 行,每行包括三个整数,S、E 和 V,代表了从 S 车站可以单向直达 E 车站,并且需要花费 V 单位的时间。

输出描述

输出一个整数,代表小明从起点到终点所花费的最小时间。

输入示例
7 9
1 2 1
1 3 4
2 3 2
2 4 5
3 4 2
4 5 3
2 6 4
5 7 4
6 7 9
输出示例
12

数据范围:

1 <= N <= 500;
1 <= M <= 5000;

[KamaCoder] 47. 参加科学大会(第六期模拟笔试)

自己看到题目的第一想法

    无

看完代码随想录之后的想法

从出发车站开始, 遍历所有的节点, 每次选取距离出发车站最近且没有访问过的车站, 并更新从该车站出发可达的车站距离出发车站的距离. 每次都挑选路径更短的作为最终的距离(路径)

import java.util.Scanner;

public class Main {
    
    private static int[][] nodes = null;
    private static int[] minDist = null;
    private static boolean[] visited = null;
    // private static int[] parent = null;
    
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int nodeCount = 0;
        while (scanner.hasNext()) {
            nodeCount = scanner.nextInt();
            int sideCount = scanner.nextInt();
            visited = new boolean[nodeCount + 1];
            // parent = new int[nodeCount + 1];
            nodes = new int[nodeCount + 1][nodeCount + 1];
            for (int i = 0; i <= nodeCount; i++) {
                for (int j = 0; j <= nodeCount; j++) {
                    nodes[i][j] = Integer.MAX_VALUE;
                }
            }
            for (int i = 0; i < sideCount; i++) {
                nodes[scanner.nextInt()][scanner.nextInt()] = scanner.nextInt();
            }
            minDist = new int[nodeCount + 1];
            for (int i = 0; i <= nodeCount; i++) {
                minDist[i] = Integer.MAX_VALUE;
            }
        }
        minDist[1] = 0;// 出发车站距离出发车站距离为 0
        int minDistIndex = 0;
        for (int i = 1; i <= nodeCount; i++) {
            minDistIndex = 0;
            for (int j = 1; j < minDist.length; j++) {
                if (!visited[j] && minDist[j] < minDist[minDistIndex]) {
                    minDistIndex = j;
                }
            }
            visited[minDistIndex] = true;
            for (int j = 1; j < nodes[minDistIndex].length; j++) {
                if (visited[j] 
                    || nodes[minDistIndex][j] == Integer.MAX_VALUE
                    || minDist[minDistIndex] + nodes[minDistIndex][j] >= minDist[j]) {
                    continue;
                }
                minDist[j] = minDist[minDistIndex] + nodes[minDistIndex][j];
                // parent[j] = minDistIndex;
            }
        }
        // for (int i = 1; i < parent.length; i++) {
        //     System.out.println(parent[i] + " -> " + i);
        // }
        if (minDist[nodeCount] == Integer.MAX_VALUE) {
            System.out.println(-1);
        } else {
            System.out.println(minDist[nodeCount]);
        }
    }
}

自己实现过程中遇到哪些困难

    无

  • 11
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
代码随想录算法训练营是一个优质的学习和讨论平台,提供了丰富的算法训练内容和讨论交流机会。在训练营中,学员们可以通过观看视频讲解来学习算法知识,并根据讲解内容进行刷题练习。此外,训练营还提供了刷题建议,例如先看视频、了解自己所使用的编程语言、使用日志等方法来提高刷题效果和语言掌握程度。 训练营中的讨论内容非常丰富,涵盖了各种算法知识点和解题方法。例如,在第14训练营中,讲解了二叉树的理论基础、递归遍历、迭代遍历和统一遍历的内容。此外,在讨论中还分享了相关的博客文章和配图,帮助学员更好地理解和掌握二叉树的遍历方法。 训练营还提供了每日的讨论知识点,例如在第15的讨论中,介绍了层序遍历的方法和使用队列来模拟一层一层遍历的效果。在第16的讨论中,重点讨论了如何进行调试(debug)的方法,认为掌握调试技巧可以帮助学员更好地解决问题和写出正确的算法代码。 总之,代码随想录算法训练营是一个提供优质学习和讨论环境的平台,可以帮助学员系统地学习算法知识,并提供了丰富的讨论内容和刷题建议来提高算法编程能力。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [代码随想录算法训练营每日精华](https://blog.csdn.net/weixin_38556197/article/details/128462133)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值