-
Notifications
You must be signed in to change notification settings - Fork 46
/
ojv.java
75 lines (67 loc) · 1.88 KB
/
ojv.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import java.io.File;
import java.util.Arrays;
import java.util.Scanner;
public class ojv {
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, nodes[0], visited);
}
final long start = System.nanoTime();
final int len = getLongestPath(0, nodes[0], visited);
final long duration = (System.nanoTime() - start) / 1000000; // ns -> ms
System.out.printf("%d LANGUAGE OracleJava %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(int nodeID, int[] row, boolean[] visited) {
visited[nodeID] = true;
int max = 0;
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, nodes[dest], visited);
if (dist > max) {
max = dist;
}
}
}
visited[nodeID] = false;
return max;
}
}