Code Practice Journal | Day59-60_Graph09 最短路径(待更)

2024-08-31 23:44

本文主要是介绍Code Practice Journal | Day59-60_Graph09 最短路径(待更),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1. Dijkstra

1.1 原理与步骤

步骤:

  1. 选取距离源点最近且未被访问过的节点
  2. 标记该节点为已访问
  3. 更新未访问节点到源点的距离
1.2 代码实现

以KamaCoder47题为例
题目:47. 参加科学大会(第六期模拟笔试) (kamacoder.com)

class Program
{public static void Main(string[] args){//处理输入string[] dimensions = Console.ReadLine().Split();int n = int.Parse(dimensions[0]);int m = int.Parse(dimensions[1]);//  visited & minDist & graphbool[] visited = new bool[n + 1];int[] minDist = new int[n + 1];int[][] graph = new int[n + 1][];for (int i = 0; i <= n; i++){graph[i] = new int[n + 1];for (int j = 0; j <= n; j++){graph[i][j] = int.MaxValue;}}//  填充for (int i = 0; i <= n; i++){visited[i] = false;if (i == 1) minDist[i] = 0;else minDist[i] = int.MaxValue;}for (int i = 0; i < m; i++){string[] edges = Console.ReadLine().Split();int start = int.Parse(edges[0]);int end = int.Parse(edges[1]);int weight = int.Parse(edges[2]);graph[start][end] = weight;}//int result = Dj(graph, n, visited, minDist);Console.WriteLine(result);}public static int Dj(int[][] graph, int n, bool[] visited, int[] minDist){for (int count = 1; count < n + 1; count++){//find min nodeint mindist = int.MaxValue;int minnode = 0;for (int i = 1; i < n + 1; i++){if (visited[i] == false){if (minDist[i] < mindist){minnode = i;mindist = minDist[i];}}}//update visitedvisited[minnode] = true;//update minDistfor (int i = 1; i < n + 1; i++){if (graph[minnode][i] != int.MaxValue){minDist[i] = Math.Min(graph[minnode][i] + mindist, minDist[i]);}}Console.WriteLine(string.Join(" ", minDist));}return minDist[n] == int.MaxValue ? -1 : minDist[n];}
}
1.3 堆优化

2. Bellman_ford

2.1 原理与步骤
2.2 代码实现
class Program
{public static void Main(string[] args){//处理输入string[] dimensions = Console.ReadLine().Split();int n = int.Parse(dimensions[0]);int m = int.Parse(dimensions[1]);//minDistint[] minDist = new int[n + 1];for (int i = 0; i <= n; i++){minDist[i] = i == 1 ? 0 : int.MaxValue;}//edgesint[][] edges = new int[m][];for (int i = 0; i < m; i++){edges[i] = new int[3];string[] edge = Console.ReadLine().Split();edges[i][0] = int.Parse(edge[0]);edges[i][1] = int.Parse(edge[1]);edges[i][2] = int.Parse(edge[2]);}//BFif (BF(edges, minDist, n - 1)){Console.WriteLine(minDist[n]);}else{Console.WriteLine("unconnected");}}public static bool BF(int[][] edges, int[] minDist, int n){bool isUpdate = true;int count = 1;while (count <= n && isUpdate == true){count++;isUpdate = false;for (int i = 0; i < edges.Length; i++){int start = edges[i][0];int end = edges[i][1];int weight = edges[i][2];if (minDist[start] != int.MaxValue){int dist = minDist[start] + weight;if (dist < minDist[end]){minDist[end] = dist;isUpdate = true;}}}}return !isUpdate;}
}
2.3 队列优化
2.4 应用场景

这篇关于Code Practice Journal | Day59-60_Graph09 最短路径(待更)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1125330

相关文章

hdu2544(单源最短路径)

模板题: //题意:求1到n的最短路径,模板题#include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<queue>#include<set>#include<map>#include<stdio.h>#include<stdlib.h>#include<ctype.h>#i

poj 1734 (floyd求最小环并打印路径)

题意: 求图中的一个最小环,并打印路径。 解析: ans 保存最小环长度。 一直wa,最后终于找到原因,inf开太大爆掉了。。。 虽然0x3f3f3f3f用memset好用,但是还是有局限性。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#incl

【408DS算法题】039进阶-判断图中路径是否存在

Index 题目分析实现总结 题目 对于给定的图G,设计函数实现判断G中是否含有从start结点到stop结点的路径。 分析实现 对于图的路径的存在性判断,有两种做法:(本文的实现均基于邻接矩阵存储方式的图) 1.图的BFS BFS的思路相对比较直观——从起始结点出发进行层次遍历,遍历过程中遇到结点i就表示存在路径start->i,故只需判断每个结点i是否就是stop

Android Environment 获取的路径问题

1. 以获取 /System 路径为例 /*** Return root of the "system" partition holding the core Android OS.* Always present and mounted read-only.*/public static @NonNull File getRootDirectory() {return DIR_ANDR

图的最短路径算法——《啊哈!算法》

图的实现方式 邻接矩阵法 int[][] map;// 图的邻接矩阵存储法map = new int[5][5];map[0] = new int[] {0, 1, 2, 3, 4};map[1] = new int[] {1, 0, 2, 6, 4};map[2] = new int[] {2, 999, 0, 3, 999};map[3] = new int[] {3, 7

vcpkg子包路径批量获取

获取vcpkg 子包的路径,并拼接为set(CMAKE_PREFIX_PATH “拼接路径” ) import osdef find_directories_with_subdirs(root_dir):# 构建根目录下的 "packages" 文件夹路径root_packages_dir = os.path.join(root_dir, "packages")# 如果 "packages"

Debugging Lua Project created in Cocos Code IDE creates “Waiting for debugger to connect” in Win-7

转自 I Installed Cocos Code IDE and created a new Lua Project. When Debugging the Project(F11) the game window pops up and gives me the message waiting for debugger to connect and then freezes. Also a

LLVM入门2:如何基于自己的代码生成IR-LLVM IR code generation实例介绍

概述 本节将通过一个简单的例子来介绍如何生成llvm IR,以Kaleidoscope IR中的例子为例,我们基于LLVM接口构建一个简单的编译器,实现简单的语句解析并转化为LLVM IR,生成对应的LLVM IR部分,代码如下,文件名为toy.cpp,先给出代码,后面会详细介绍每一步分代码: #include "llvm/ADT/APFloat.h"#include "llvm/ADT/S

VS Code 调试go程序的相关配置说明

用 VS code 调试Go程序需要在.vscode/launch.json文件中增加如下配置:  // launch.json{// Use IntelliSense to learn about possible attributes.// Hover to view descriptions of existing attributes.// For more information,

jmeter依赖jar包找不到类路径

这两天我在纠结这个问题,为啥我maven打包放在jmeter路径下后,jmeter的bean Shell 就是找不到这个类。纠结很久解开了。我记录下,留给后来的朋友。   Error invoking bsh method: eval Sourced file: inline evaluation of: ``import org.apache.jmeter.protocol.http.s