news 2026/4/18 5:31:59

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/4/16 15:45:54

GLM-4V-9B 4-bit量化技术解析:QLoRA微调兼容性与精度保留实测

GLM-4V-9B 4-bit量化技术解析&#xff1a;QLoRA微调兼容性与精度保留实测 1. 为什么需要4-bit量化&#xff1f;从显存瓶颈说起 你有没有试过在自己的笔记本上跑多模态大模型&#xff1f;刚下载完GLM-4V-9B&#xff0c;一加载就报错“CUDA out of memory”——这几乎是每个想本…

作者头像 李华
网站建设 2026/4/16 21:36:45

掌握DLSS版本管理技巧与性能优化的艺术

掌握DLSS版本管理技巧与性能优化的艺术 【免费下载链接】dlss-swapper 项目地址: https://gitcode.com/GitHub_Trending/dl/dlss-swapper DLSS&#xff08;深度学习超级采样&#xff09;技术作为提升游戏画质与帧率的关键工具&#xff0c;其版本兼容性直接影响游戏体验…

作者头像 李华
网站建设 2026/4/12 17:24:07

Chatbot AI 开发实战:从零构建高可用对话系统的避坑指南

Chatbot AI 开发实战&#xff1a;从零构建高可用对话系统的避坑指南 痛点分析&#xff1a;为什么我的机器人总把“我要退款”听成“我要鸡腿”&#xff1f; 意图识别准确率忽高忽低 线上日志显示&#xff0c;用户说“我不想买了”被误判成“查询订单”&#xff0c;结果直接弹出…

作者头像 李华
网站建设 2026/4/8 11:29:46

如何下载并加载YOLOv12n.pt权重文件?

如何下载并加载YOLOv12n.pt权重文件&#xff1f; 在目标检测领域&#xff0c;模型权重的获取与加载是实际应用的第一步。对于刚接触 YOLOv12 的开发者来说&#xff0c;一个常见困惑是&#xff1a;“yolov12n.pt 到底从哪来&#xff1f;需要手动下载吗&#xff1f;能不能直接用…

作者头像 李华
网站建设 2026/4/3 14:43:08

无需GPU知识!一键启动VibeVoice做播客级音频

无需GPU知识&#xff01;一键启动VibeVoice做播客级音频 在内容创作越来越依赖AI的今天&#xff0c;很多人想做播客、有声书或教学音频&#xff0c;却被卡在第一步&#xff1a;怎么把文字变成自然、有情绪、带角色的语音&#xff1f; 不是声音太机械&#xff0c;就是操作太复杂…

作者头像 李华