import java.io.File;
import java.util.Arrays;
import java.util.Scanner;

public class jv {
	private static int numNodes = -1;
	private static final int[][] nodes;
	private static final boolean[] visited;
	
	static {
		nodes = readPlaces();
		visited = new boolean[numNodes];
	}
	
	public static void main(final String[] args) {
		// warm up a bit
		for (int i = 0; i < 10; i++) {
			getLongestPath(0, visited);
		}
		
		final long start = System.nanoTime();
		final int len = getLongestPath(0, visited);
		final long duration = (System.nanoTime() - start) / 1000000; // ns -> ms
		System.out.printf("%d LANGUAGE Java %d\n", len, duration);
	}
	
	/**
	 * int[node][dest|cost|...]
	 */
	private static int[][] readPlaces() {
		try (Scanner in = new Scanner(new File("agraph"))) {
			numNodes = in.nextInt();
			int[][] nodes = new int[numNodes][];
			for (int i = 0; i < numNodes; i++) {
				nodes[i] = new int[0];
			}
			while (in.hasNextInt()) {
				int node = in.nextInt();
				int neighbour = in.nextInt();
				int cost = in.nextInt();
				int index = nodes[node].length;
				int[] replacement = Arrays.copyOf(nodes[node], index + 2);
				replacement[index] = neighbour;
				replacement[index + 1] = cost;
				nodes[node] = replacement;
			}
			// reduce any fragmentation
			int[][] newNodes = new int[numNodes][];
			for (int i = 0; i < numNodes; i++) {
				newNodes[i] = Arrays.copyOf(nodes[i], nodes[i].length);
			}
			return newNodes;
		} catch (Exception e) {
			return null;
		}
	}
	
	private static int getLongestPath(final int nodeID, boolean[] visited) {
		visited[nodeID] = true;
		int max = 0;
		
		final int[] row = nodes[nodeID];
		for (int i = 0; i < row.length; i += 2) {
			final int dest = row[i];
			if (!visited[dest]) {
				final int dist = row[i + 1] + getLongestPath(dest, visited);
				if (dist > max) {
					max = dist;
				}
			}
		}
		
		visited[nodeID] = false;
		return max;
	}
}

@@ -385,32 +385,42 @@
 etLongestPath(0,
+ nodes%5B0%5D,
  visited);%0A%09%09%7D%0A%09
@@ -496,16 +496,26 @@
 tPath(0,
+ nodes%5B0%5D,
  visited
@@ -1543,22 +1543,16 @@
 estPath(
-final 
 int node
@@ -1554,16 +1554,27 @@
  nodeID,
+ int%5B%5D row,
  boolean
@@ -1635,43 +1635,8 @@
 %0A%09%09%0A
-%09%09final int%5B%5D row = nodes%5BnodeID%5D;%0A
 %09%09fo
@@ -1782,16 +1782,29 @@
 th(dest,
+ nodes%5Bdest%5D,
  visited

Update jv.java