news 2026/4/16 16:21:27

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 13:39:05

3个超实用技巧:用PCL2实现Minecraft高效管理解决方案

3个超实用技巧&#xff1a;用PCL2实现Minecraft高效管理解决方案 【免费下载链接】PCL2 项目地址: https://gitcode.com/gh_mirrors/pc/PCL2 你是否经常遇到Minecraft启动器崩溃、模组安装混乱、多账号切换繁琐的问题&#xff1f;Plain Craft Launcher 2&#xff08;简…

作者头像 李华
网站建设 2026/3/30 3:09:24

Z-Image-ComfyUI生成带文字图片,中英文都清晰

Z-Image-ComfyUI生成带文字图片&#xff0c;中英文都清晰 在AI图像生成的实际使用中&#xff0c;你是否也遇到过这些尴尬时刻&#xff1f; 输入“北京故宫雪景&#xff0c;红墙金瓦&#xff0c;中文标题‘瑞雪兆丰年’”&#xff0c;结果标题位置歪斜、字体模糊、笔画粘连&…

作者头像 李华
网站建设 2026/4/16 13:03:57

软件本地化工具安装教程:界面汉化与配置全指南

软件本地化工具安装教程&#xff1a;界面汉化与配置全指南 【免费下载链接】AndroidStudioChineseLanguagePack AndroidStudio中文插件(官方修改版本&#xff09; 项目地址: https://gitcode.com/gh_mirrors/an/AndroidStudioChineseLanguagePack 软件本地化是将应用程序…

作者头像 李华
网站建设 2026/4/16 13:04:09

YOLOE镜像环境配置详解,Conda环境轻松激活

YOLOE镜像环境配置详解&#xff0c;Conda环境轻松激活 YOLOE不是又一个“YOLO变体”的简单堆砌&#xff0c;而是一次对开放世界视觉理解范式的重新定义。当大多数模型还在为固定类别列表反复训练时&#xff0c;YOLOE已经能对着一张陌生街景照片&#xff0c;准确圈出“滑板少年…

作者头像 李华
网站建设 2026/4/16 12:20:26

STM32与cJSON实战:从零构建嵌入式JSON数据解析方案

1. 为什么STM32需要JSON解析能力 在物联网和嵌入式设备爆发式增长的今天&#xff0c;JSON已经成为设备间通信的事实标准格式。我刚开始接触STM32与云平台对接时&#xff0c;发现大多数API接口都采用JSON格式传输数据。比如一个简单的温湿度传感器数据&#xff0c;云端通常要求这…

作者头像 李华