news 2026/2/7 9:59:26

Day60 >> 94、城市间货物运输1️⃣ + 95、城市间货物运输 2️⃣ + 96、城市间货物运输 3️⃣

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Day60 >> 94、城市间货物运输1️⃣ + 95、城市间货物运输 2️⃣ + 96、城市间货物运输 3️⃣

代码随想录-图论Part10

94、城市间货物运输 1️⃣

Bellman_ford队列优化算法

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<List<Edge>> 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<Integer> 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]); } } }

95、城市间货物运输 2️⃣

Bellman_ford判断负权回路

import java.util.*; public class Main { // 基于Bellman_ford-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<List<Edge>> 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<Integer> 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]); } } }

96、城市间货物运输 3️⃣

Bellman_ford单源有限最短路

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<Edge> 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]); } } }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/2/4 6:41:37

跨界融合:3D模型到方块世界的创意落地全指南

跨界融合&#xff1a;3D模型到方块世界的创意落地全指南 【免费下载链接】ObjToSchematic A tool to convert 3D models into Minecraft formats such as .schematic, .litematic, .schem and .nbt 项目地址: https://gitcode.com/gh_mirrors/ob/ObjToSchematic 当你精心…

作者头像 李华
网站建设 2026/2/3 1:15:00

从零开始:用ccmusic-database/music_genre打造音乐分类器

从零开始&#xff1a;用ccmusic-database/music_genre打造音乐分类器 你有没有过这样的困惑&#xff1a;听到一首歌&#xff0c;旋律很熟悉&#xff0c;但就是说不准它属于什么流派&#xff1f;是爵士还是蓝调&#xff1f;是电子还是拉丁&#xff1f;又或者&#xff0c;你正在…

作者头像 李华
网站建设 2026/2/3 1:14:58

GLM-4V-9B图文对话入门必看:消费级显卡流畅运行完整指南

GLM-4V-9B图文对话入门必看&#xff1a;消费级显卡流畅运行完整指南 1. 为什么你需要关注GLM-4V-9B&#xff1f; 你是不是也遇到过这样的困扰&#xff1a;想本地跑一个能“看图说话”的AI模型&#xff0c;但一查显存要求就打退堂鼓&#xff1f;官方文档写着“推荐24G显存”&a…

作者头像 李华
网站建设 2026/2/3 1:14:41

如何通过OpenCore Legacy Patcher让老旧Mac重获新生

如何通过OpenCore Legacy Patcher让老旧Mac重获新生 【免费下载链接】OpenCore-Legacy-Patcher 体验与之前一样的macOS 项目地址: https://gitcode.com/GitHub_Trending/op/OpenCore-Legacy-Patcher 你是否曾经遇到这样的情况&#xff1a;手中的Mac设备还能正常使用&…

作者头像 李华