From 00405e519283b681daca6bb0f9dc5d292c1cc9ab Mon Sep 17 00:00:00 2001 From: Allen Guo Date: Wed, 7 Aug 2024 07:13:31 -0700 Subject: [PATCH 1/5] =?UTF-8?q?=E6=9B=B4=E6=96=B00094.=E5=9F=8E=E5=B8=82?= =?UTF-8?q?=E9=97=B4=E8=B4=A7=E7=89=A9=E8=BF=90=E8=BE=93I.md=20Java?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kamacoder/0094.城市间货物运输I.md | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/problems/kamacoder/0094.城市间货物运输I.md b/problems/kamacoder/0094.城市间货物运输I.md index f72bfc00..45ca1313 100644 --- a/problems/kamacoder/0094.城市间货物运输I.md +++ b/problems/kamacoder/0094.城市间货物运输I.md @@ -392,6 +392,63 @@ Bellman_ford 是可以计算 负权值的单源最短路算法。 ## 其他语言版本 ### Java +```Java +public class Main { + + // Define an inner class Edge + static class Edge { + int from; + int to; + int val; + public Edge(int from, int to, int val) { + this.from = from; + this.to = to; + this.val = val; + } + } + + public static void main(String[] args) { + // Input processing + Scanner sc = new Scanner(System.in); + int n = sc.nextInt(); + int m = sc.nextInt(); + List edges = new ArrayList<>(); + + for (int i = 0; i < m; i++) { + int from = sc.nextInt(); + int to = sc.nextInt(); + int val = sc.nextInt(); + edges.add(new Edge(from, to, val)); + } + + // Represents the minimum distance from the current node to the original node + int[] minDist = new int[n + 1]; + + // Initialize the minDist array + Arrays.fill(minDist, Integer.MAX_VALUE); + minDist[1] = 0; + + // Starts the loop to relax all edges n - 1 times to update minDist array + for (int i = 1; i < n; i++) { + + for (Edge edge : edges) { + // Updates the minDist array + if (minDist[edge.from] != Integer.MAX_VALUE && (minDist[edge.from] + edge.val) < minDist[edge.to]) { + minDist[edge.to] = minDist[edge.from] + edge.val; + } + } + } + + // Outcome printing + if (minDist[n] == Integer.MAX_VALUE) { + System.out.println("unconnected"); + } else { + System.out.println(minDist[n]); + } + } +} + +``` ### Python From 2ecb6a5e71a40e6cae9f02c8bb0755f4ba8249d0 Mon Sep 17 00:00:00 2001 From: Allen Guo Date: Wed, 7 Aug 2024 10:16:28 -0700 Subject: [PATCH 2/5] =?UTF-8?q?=E6=9B=B4=E6=96=B00094.=E5=9F=8E=E5=B8=82?= =?UTF-8?q?=E9=97=B4=E8=B4=A7=E7=89=A9=E8=BF=90=E8=BE=93I-SPFA=20Java?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0094.城市间货物运输I-SPFA.md | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/problems/kamacoder/0094.城市间货物运输I-SPFA.md b/problems/kamacoder/0094.城市间货物运输I-SPFA.md index 07317d6b..ce383919 100644 --- a/problems/kamacoder/0094.城市间货物运输I-SPFA.md +++ b/problems/kamacoder/0094.城市间货物运输I-SPFA.md @@ -352,6 +352,77 @@ SPFA(队列优化版Bellman_ford) 在理论上 时间复杂度更胜一筹 ## 其他语言版本 ### Java +```Java +import java.util.*; + +public class Main { + + // Define an inner class Edge + static class Edge { + int from; + int to; + int val; + public Edge(int from, int to, int val) { + this.from = from; + this.to = to; + this.val = val; + } + } + + public static void main(String[] args) { + // Input processing + Scanner sc = new Scanner(System.in); + int n = sc.nextInt(); + int m = sc.nextInt(); + List> graph = new ArrayList<>(); + + for (int i = 0; i <= n; i++) { + graph.add(new ArrayList<>()); + } + + for (int i = 0; i < m; i++) { + int from = sc.nextInt(); + int to = sc.nextInt(); + int val = sc.nextInt(); + graph.get(from).add(new Edge(from, to, val)); + } + + // Declare the minDist array to record the minimum distance form current node to the original node + int[] minDist = new int[n + 1]; + Arrays.fill(minDist, Integer.MAX_VALUE); + minDist[1] = 0; + + // Declare a queue to store the updated nodes instead of traversing all nodes each loop for more efficiency + Queue queue = new LinkedList<>(); + queue.offer(1); + + // Declare a boolean array to record if the current node is in the queue to optimise the processing + boolean[] isInQueue = new boolean[n + 1]; + + while (!queue.isEmpty()) { + int curNode = queue.poll(); + isInQueue[curNode] = false; // Represents the current node is not in the queue after being polled + for (Edge edge : graph.get(curNode)) { + if (minDist[edge.to] > minDist[edge.from] + edge.val) { // Start relaxing the edge + minDist[edge.to] = minDist[edge.from] + edge.val; + if (!isInQueue[edge.to]) { // Don't add the node if it's already in the queue + queue.offer(edge.to); + isInQueue[edge.to] = true; + } + } + } + } + + // Outcome printing + if (minDist[n] == Integer.MAX_VALUE) { + System.out.println("unconnected"); + } else { + System.out.println(minDist[n]); + } + } +} + +``` ### Python From 047c57ab4063945aeca9b1032fe74e8f5d14a4e8 Mon Sep 17 00:00:00 2001 From: Allen Guo Date: Thu, 8 Aug 2024 09:34:28 -0700 Subject: [PATCH 3/5] =?UTF-8?q?=E6=9B=B4=E6=96=B00095.=E5=9F=8E=E5=B8=82?= =?UTF-8?q?=E9=97=B4=E8=B4=A7=E7=89=A9=E8=BF=90=E8=BE=93II=20=E5=9F=BA?= =?UTF-8?q?=E4=BA=8EDPFA=E7=89=88=E6=9C=AC=E7=9A=84Java=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kamacoder/0095.城市间货物运输II.md | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/problems/kamacoder/0095.城市间货物运输II.md b/problems/kamacoder/0095.城市间货物运输II.md index 5bc4be7b..6226eb01 100644 --- a/problems/kamacoder/0095.城市间货物运输II.md +++ b/problems/kamacoder/0095.城市间货物运输II.md @@ -244,6 +244,92 @@ int main() { ## 其他语言版本 ### Java +```Java +import java.util.*; + +public class Main { + // 基于SPFA方法 + // Define an inner class Edge + static class Edge { + int from; + int to; + int val; + public Edge(int from, int to, int val) { + this.from = from; + this.to = to; + this.val = val; + } + } + + public static void main(String[] args) { + // Input processing + Scanner sc = new Scanner(System.in); + int n = sc.nextInt(); + int m = sc.nextInt(); + List> graph = new ArrayList<>(); + + for (int i = 0; i <= n; i++) { + graph.add(new ArrayList<>()); + } + + for (int i = 0; i < m; i++) { + int from = sc.nextInt(); + int to = sc.nextInt(); + int val = sc.nextInt(); + graph.get(from).add(new Edge(from, to, val)); + } + + // Declare the minDist array to record the minimum distance form current node to the original node + int[] minDist = new int[n + 1]; + Arrays.fill(minDist, Integer.MAX_VALUE); + minDist[1] = 0; + + // Declare a queue to store the updated nodes instead of traversing all nodes each loop for more efficiency + Queue queue = new LinkedList<>(); + queue.offer(1); + + // Declare an array to record the times each node has been offered in the queue + int[] count = new int[n + 1]; + count[1]++; + + // Declare a boolean array to record if the current node is in the queue to optimise the processing + boolean[] isInQueue = new boolean[n + 1]; + + // Declare a boolean value to check if there is a negative weight loop inside the graph + boolean flag = false; + + while (!queue.isEmpty()) { + int curNode = queue.poll(); + isInQueue[curNode] = false; // Represents the current node is not in the queue after being polled + for (Edge edge : graph.get(curNode)) { + if (minDist[edge.to] > minDist[edge.from] + edge.val) { // Start relaxing the edge + minDist[edge.to] = minDist[edge.from] + edge.val; + if (!isInQueue[edge.to]) { // Don't add the node if it's already in the queue + queue.offer(edge.to); + count[edge.to]++; + isInQueue[edge.to] = true; + } + + if (count[edge.to] == n) { // If some node has been offered in the queue more than n-1 times + flag = true; + while (!queue.isEmpty()) queue.poll(); + break; + } + } + } + } + + if (flag) { + System.out.println("circle"); + } else if (minDist[n] == Integer.MAX_VALUE) { + System.out.println("unconnected"); + } else { + System.out.println(minDist[n]); + } + } +} + +``` ### Python From d2163ac3f698a4f2f0c4077a40db4e81cab42e14 Mon Sep 17 00:00:00 2001 From: Allen Guo Date: Thu, 8 Aug 2024 09:43:47 -0700 Subject: [PATCH 4/5] =?UTF-8?q?=E6=9B=B4=E6=96=B00095.=E5=9F=8E=E5=B8=82?= =?UTF-8?q?=E9=97=B4=E8=B4=A7=E7=89=A9=E8=BF=90=E8=BE=93II=20=E5=9F=BA?= =?UTF-8?q?=E4=BA=8ESPFA=E7=89=88=E6=9C=AC=E7=9A=84Java=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/kamacoder/0095.城市间货物运输II.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/kamacoder/0095.城市间货物运输II.md b/problems/kamacoder/0095.城市间货物运输II.md index 6226eb01..f1025e4a 100644 --- a/problems/kamacoder/0095.城市间货物运输II.md +++ b/problems/kamacoder/0095.城市间货物运输II.md @@ -248,7 +248,7 @@ int main() { import java.util.*; public class Main { - // 基于SPFA方法 + // 基于Bellman_ford-SPFA方法 // Define an inner class Edge static class Edge { int from; From ab6cb84cb905dc3ededa4165653435afd38c34ed Mon Sep 17 00:00:00 2001 From: Allen Guo Date: Thu, 8 Aug 2024 17:14:40 -0700 Subject: [PATCH 5/5] =?UTF-8?q?=E6=9B=B4=E6=96=B00096.=E5=9F=8E=E5=B8=82?= =?UTF-8?q?=E9=97=B4=E8=B4=A7=E7=89=A9=E8=BF=90=E8=BE=93III=20=E5=9F=BA?= =?UTF-8?q?=E4=BA=8EBellman=5Fford=E4=B8=80=E8=88=AC=E8=A7=A3=E6=B3=95Java?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0096.城市间货物运输III.md | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/problems/kamacoder/0096.城市间货物运输III.md b/problems/kamacoder/0096.城市间货物运输III.md index feaaa1f3..dacd23d1 100644 --- a/problems/kamacoder/0096.城市间货物运输III.md +++ b/problems/kamacoder/0096.城市间货物运输III.md @@ -636,6 +636,71 @@ dijkstra 是贪心的思路 每一次搜索都只会找距离源点最近的非 ## 其他语言版本 ### Java +```Java +import java.util.*; + +public class Main { + // 基于Bellman_for一般解法解决单源最短路径问题 + // Define an inner class Edge + static class Edge { + int from; + int to; + int val; + public Edge(int from, int to, int val) { + this.from = from; + this.to = to; + this.val = val; + } + } + + public static void main(String[] args) { + // Input processing + Scanner sc = new Scanner(System.in); + int n = sc.nextInt(); + int m = sc.nextInt(); + + List graph = new ArrayList<>(); + + for (int i = 0; i < m; i++) { + int from = sc.nextInt(); + int to = sc.nextInt(); + int val = sc.nextInt(); + graph.add(new Edge(from, to, val)); + } + + int src = sc.nextInt(); + int dst = sc.nextInt(); + int k = sc.nextInt(); + + int[] minDist = new int[n + 1]; + int[] minDistCopy; + + Arrays.fill(minDist, Integer.MAX_VALUE); + minDist[src] = 0; + + for (int i = 0; i < k + 1; i++) { // Relax all edges k + 1 times + minDistCopy = Arrays.copyOf(minDist, n + 1); + for (Edge edge : graph) { + int from = edge.from; + int to = edge.to; + int val = edge.val; + // Use minDistCopy to calculate minDist + if (minDistCopy[from] != Integer.MAX_VALUE && minDist[to] > minDistCopy[from] + val) { + minDist[to] = minDistCopy[from] + val; + } + } + } + + // Output printing + if (minDist[dst] == Integer.MAX_VALUE) { + System.out.println("unreachable"); + } else { + System.out.println(minDist[dst]); + } + } +} + +``` ### Python