Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

dijkstra java

import java.util.*;
import java.io.*;
public class DijkstraPQ {
    static void dijkstra (int start, List<Edge> adj[], int dis[]) {
        Arrays.fill(dis, Integer.MAX_VALUE); dis[start] = 0;
        PriorityQueue<Edge> q = new PriorityQueue<>();
        q.add(new Edge(start, dis[start]));
        while(!q.isEmpty()) {
            int u = q.peek().v; long d = q.peek().w; q.poll();
            if(d > dis[u]) continue;
            for(Edge e : adj[u]) {
                if(dis[e.v] > dis[u] + e.w) {
                    dis[e.v] = dis[u] + e.w; q.add(new Edge(e.v, dis[e.v]));
                }
            }
        }
    }

    static class Edge implements Comparable <Edge> {
        int v, w;
        public Edge(int v, int w) {this.v = v; this.w = w;}
        public int compareTo(Edge e) {return w - e.w;}
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: 2d arraylist in java 
Java :: clear text field in java 
Java :: initialize arraylist in 1 line in java 
Java :: how to read input in java 
Java :: clear jtable rows java 
Java :: add stringarrays to string list java 
Java :: counting nodes in binary search tree 
Java :: how to declare an array in java 
Java :: java copy array 
Java :: java replaceall regex 
Java :: how to write deserlizer java 
Java :: jframe change icon 
Java :: how to create relative layout in android studio 
Java :: java remove trailing zeros from float 
Java :: select photo from camera android 
Java :: literals in java 
Java :: array contains java 
Java :: Java how to make a number with digits 
Java :: java list distinct by object attribute 
Java :: input array in String in java 
Java :: java exception message 
Java :: how to print to the console in java 
Java :: java float 0/0 
Java :: java while loop example 
Java :: integer to string java 
Java :: java loop through list 
Java :: abstract class in java 
Java :: array sort java 
Java :: java create unmodifiable list 
Java :: java enum length 
ADD CONTENT
Topic
Content
Source link
Name
4+6 =