news 2026/9/30 16:14:34

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/9/30 12:55:23

VibeVoice Pro语音合成安全:语音内容审核API集成方案

VibeVoice Pro语音合成安全&#xff1a;语音内容审核API集成方案 1. 为什么语音合成需要内容审核&#xff1f; 你有没有想过&#xff0c;当AI能几秒内生成一段以假乱真的真人语音时&#xff0c;风险也同步放大了&#xff1f;不是所有文字都适合变成声音——比如恶意诱导、虚假…

作者头像 李华
网站建设 2026/9/29 5:46:24

量子硬件接口开发避坑清单:97.3%的C程序员在qubit校准阶段踩过的7个ABI陷阱(含TI Quantum SDK v2.1补丁包)

第一章&#xff1a;量子硬件接口开发避坑总纲与ABI陷阱认知框架 量子硬件接口开发并非传统驱动开发的简单延伸&#xff0c;其核心挑战在于跨抽象层的语义断裂&#xff1a;量子指令集&#xff08;QIS&#xff09;与经典运行时环境之间缺乏稳定、可验证的二进制接口契约。开发者常…

作者头像 李华
网站建设 2026/9/29 5:46:22

如何在Windows系统使用AirPods管理工具提升耳机使用体验

如何在Windows系统使用AirPods管理工具提升耳机使用体验 【免费下载链接】AirPodsDesktop ☄️ AirPods desktop user experience enhancement program, for Windows and Linux (WIP) 项目地址: https://gitcode.com/gh_mirrors/ai/AirPodsDesktop AirPodsDesktop是一款…

作者头像 李华
网站建设 2026/9/29 11:27:36

Mac运行iOS应用完全指南:解锁跨平台桌面化体验新可能

Mac运行iOS应用完全指南&#xff1a;解锁跨平台桌面化体验新可能 【免费下载链接】PlayCover Community fork of PlayCover 项目地址: https://gitcode.com/gh_mirrors/pl/PlayCover 在数字生活中&#xff0c;我们常常面临这样的困境&#xff1a;手机上的精彩应用受限于…

作者头像 李华
网站建设 2026/9/29 8:25:23

零基础入门智能剪辑:Autocut高效制作新手教程

零基础入门智能剪辑&#xff1a;Autocut高效制作新手教程 【免费下载链接】autocut 用文本编辑器剪视频 项目地址: https://gitcode.com/GitHub_Trending/au/autocut 在短视频内容井喷的时代&#xff0c;视频剪辑效率成为创作者的核心竞争力。然而传统剪辑软件复杂的操作…

作者头像 李华