// Dijkstra's Algorithm in Java using a heap with the best S vertex for // every T vertex. // See CSE 2320 Notes 16. import java.util.Scanner; public class dijkstraHeap { public static class dijkstraFails extends Exception { public dijkstraFails() { super(); } public dijkstraFails(String message) { super(message); } } final static int oo=(1000000000); // Declarations enum st {s,t}; static int numVertices,numEdges; static class edge { int tail,head,weight; int next; // subscript of next edge with same tail } static edge[] edgeTab; static int[] firstEdge; // Table indicating first edge for a tail static void read_input_file() throws dijkstraFails { // Reads a file with a weighted, directed graph and // stores as unordered adjacency lists. int tail,head,weight,i,j,workingEdges; // read number of nodes and edges Scanner sc=new Scanner(System.in); numVertices=sc.nextInt(); numEdges=sc.nextInt(); firstEdge=new int[numVertices]; edgeTab=new edge[numEdges]; for (i=0;inumVertices-1 || head<0 || head>numVertices-1 || weight<=0) throw new dijkstraFails("Invalid input "+tail+" "+head+" "+ weight); // Save input edge and insert in adjacency list edgeTab[workingEdges].tail=tail; edgeTab[workingEdges].head=head; edgeTab[workingEdges].weight=weight; edgeTab[workingEdges].next=firstEdge[tail]; firstEdge[tail]=workingEdges; workingEdges++; } } static void dijkstra() throws dijkstraFails,minHeap.minHeapFails { int i,j,SPedgeCount=0; st[] flag=new st[numVertices]; heapEntry smallest, // reference is returned from minHeap changing=new heapEntry(); // gets passed to minHeap int[] priority=new int[numVertices], bestSneighbor=new int[numVertices]; // Vertex 0 starts in S. Other vertices start in T. flag[0]=st.s; for (i=1;i