Skip to content

搞英语 → 看世界

翻译英文优质信息和名人推特

Menu
  • 首页
  • 作者列表
  • 独立博客
  • 专业媒体
  • 名人推特
  • 邮件列表
  • 关于本站
Menu

Dijkstra 的Java 代码面临的问题||力码

Posted on 2022-05-30

1099327.png

当我解决关于 Dijkstra 的问题时,我基本上有两种方式(或 Java 模板)来为 Dijkstra 编写代码,但对于某些问题,问题通过两种方式解决(或 Dijkstra 的 Java 模板)( https://leetcode.com/ questions /network-delay-time/ ) 被接受,并且对于某些(例如, https ://leetcode.com/problems/path-with-minimum-effort/ ),任何人都可以解决这个问题。

对于图(在邻接列表中表示):

 ArrayList<int[]>[] graph = new ArrayList[n]; // n represents number of nodes.

1. Dijkstra’s – 一种方式

boolean[] vis = new boolean[n]; int[] dist = new int[n]; Arrays.fill( dist, Integer.MAX_VALUE ); PriorityQueue<Integer> q = new PriorityQueue<>( (a,b) -> dist[a] - dist[b] ); q.add( 0 ); // Starting node dist[start] = 0; while( !q.isEmpty() ) { int node = q.poll(); if( vis[node] ) continue; vis[node] = true; // traversing neighboours for( int[] nb : graph[node] ) { int node2 = nb[0]; int weight = nb[1]; if( !vis[node2] && dist[node2] > dist[node] + weight ) { dist[node2] = dist[node] + weight; q.add( node2 ); } } }

2. Dijkstra’s – 第二种方式

 boolean[] vis = new boolean[n]; int[] dist = new int[n]; Arrays.fill( dist, Integer.MAX_VALUE ); PriorityQueue<int[]> q = new PriorityQueue<>( (a,b) -> a[1] - b[1] ); q.add( new int[2] ); // Starting node dist[start] = 0; while( !q.isEmpty() ) { int node = q.peek()[0]; int dis = q.peek()[1]; if( vis[node] ) continue; vis[node] = true; // traversing neighboours for( int[] nb : graph[node] ) { int node2 = nb[0]; int weight = nb[1]; if( !vis[node2] && dist[node2] > dis + weight ) { dist[node2] = dis + weight; q.add( new int[] { node2, dist[node2] } ); } } }

任何人都可以帮助我知道哪种方法是正确的(第一种或第二种)。

原文: https://dev.to/nihalagarwal/dijkstras-java-code-5can

本站文章系自动翻译,站长会周期检查,如果有不当内容,请点此留言,非常感谢。
  • Abhinav
  • Abigail Pain
  • Adam Fortuna
  • Alberto Gallego
  • Alex Wlchan
  • Answer.AI
  • Arne Bahlo
  • Ben Carlson
  • Ben Kuhn
  • Bert Hubert
  • Bits about Money
  • Brian Krebs
  • ByteByteGo
  • Chip Huyen
  • Chips and Cheese
  • Christopher Butler
  • Cool Infographics
  • Dan Sinker
  • David Walsh
  • Dmitry Dolzhenko
  • Elad Gil
  • Ellie Huxtable
  • Ethan Marcotte
  • Exponential View
  • FAIL Blog
  • Founder Weekly
  • Geoffrey Huntley
  • Geoffrey Litt
  • Greg Mankiw
  • Henrique Dias
  • Hypercritical
  • IEEE Spectrum
  • Investment Talk
  • Jaz
  • Jeff Geerling
  • Jonas Hietala
  • Josh Comeau
  • Lenny Rachitsky
  • Lou Plummer
  • Luke Wroblewski
  • Matt Stoller
  • Mert Bulan
  • Mostly metrics
  • News Letter
  • NextDraft
  • Non_Interactive
  • Not Boring
  • One Useful Thing
  • Phil Eaton
  • Product Market Fit
  • Readwise
  • ReedyBear
  • Robert Heaton
  • Ruben Schade
  • Sage Economics
  • Sam Altman
  • Sam Rose
  • selfh.st
  • Shtetl-Optimized
  • Simon schreibt
  • Slashdot
  • Small Good Things
  • Taylor Troesh
  • Telegram Blog
  • The Macro Compass
  • The Pomp Letter
  • thesephist
  • Thinking Deep & Wide
  • Tim Kellogg
  • 英文媒体
  • 英文推特
  • 英文独立博客
©2025 搞英语 → 看世界 | Design: Newspaperly WordPress Theme