题目描述
某个大型软件项目的构建系统拥有 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]);
}
}
}
自己实现过程中遇到哪些困难
无