forked from logicchains/LPATHBench
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathojv.java
More file actions
79 lines (65 loc) · 1.82 KB
/
ojv.java
File metadata and controls
79 lines (65 loc) · 1.82 KB
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
76
77
78
79
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Arrays;
public class ojv{
static int numNodes = -1;
static final int[][] nodes;
static final boolean[] visited;
static {
nodes = readPlaces();
visited = new boolean[numNodes];
}
public static void main(final String[] args) throws Exception{
final long start = System.currentTimeMillis();
final int len = getLongestPath(0);
final long duration = System.currentTimeMillis() - start;
System.out.printf("%d LANGUAGE OracleJava %d\n", len, duration);
}
/**
* int[node][dest|cost|...]
*/
static int[][] readPlaces() {
try (BufferedReader in = new BufferedReader(new FileReader("agraph"))) {
final String s = in.readLine();
numNodes = Integer.parseInt(s);
final int[][] nodes = new int[numNodes][];
for(int i = 0; i < numNodes; i++){
nodes[i]= new int[0];
}
while (in.ready()) {
final String ln = in.readLine();
final String[] nums = ln.split("[ \t]+");
if(nums.length != 3){
break;
}
final int node = Integer.parseInt(nums[0]);
final int neighbour = Integer.parseInt(nums[1]);
final int cost = Integer.parseInt(nums[2]);
final int index = nodes[node].length;
final int[] replacement = Arrays.copyOf(nodes[node], index + 2);
replacement[index] = neighbour;
replacement[index+1] = cost;
nodes[node] = replacement;
}
return nodes;
} catch (Exception e) {
return null;
}
}
static int getLongestPath(final int nodeID){
visited[nodeID] = true;
int dist, max=0;
final int length = nodes[nodeID].length;
for (int i = 0; i < length; i+=2) {
final int dest = nodes[nodeID][i];
if (!visited[dest]) {
dist = nodes[nodeID][i + 1] + getLongestPath(dest);
if (dist > max) {
max = dist;
}
}
}
visited[nodeID] = false;
return max;
}
}