Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sun.misc.Unsafe;
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.lang.reflect.Field;
- import java.util.Arrays;
- /**
- * Uses unsafe and compacts the node data into a dense representation
- *
- * Nodes representation:
- *
- * int rowlength
- *
- * dest | cost | 0 0 0 0 0 ... 0 0
- * dest | cost | x x x x x ... 0 0
- *
- * Testing for zero is marginally faster, exploit this instead of checking against array position against rowlength
- *
- *
- */
- public class jv{
- final static byte ZERO = (byte) 0;
- final static byte ONE = (byte) 1;
- static final Unsafe UNSAFE = getUnsafe();
- static int numNodes;
- static int rowlength;
- static final long ADR_nodes;
- static final long ADR_visited;
- static {
- ADR_nodes = readPlaces();
- ADR_visited = UNSAFE.allocateMemory(numNodes);
- for (int i = 0; i < numNodes; i++) {
- UNSAFE.putByte(ADR_visited + i, ZERO);
- }
- }
- 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 Java %d\n", len, duration);
- }
- static long 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];
- }
- rowlength = 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;
- rowlength = Math.max(rowlength, index + 2);
- }
- // convert nodes to dense format and write to raw memory
- rowlength += 2;
- rowlength *= 4;
- final long address = UNSAFE.allocateMemory(numNodes * rowlength);
- for (int i = 0; i < numNodes * rowlength; i++) {
- UNSAFE.putByte(address + i, ZERO);
- }
- for (int i = 0; i < nodes.length; i++) {
- for (int j = 0; j < nodes[i].length; j++) {
- UNSAFE.putInt(address + (i * rowlength) + (j * 4), nodes[i][j]);
- }
- }
- return address;
- } catch (Exception e) {
- return 0;
- }
- }
- static int getLongestPath(final int nodeID){
- UNSAFE.putByte(ADR_visited + nodeID, ONE);
- int dist, max=0;
- final long ADR_row = ADR_nodes + (rowlength * nodeID);
- int cost;
- for (int i = 0; (cost = UNSAFE.getInt(ADR_row + i + 4)) != 0; i+=8) {
- final int dest = UNSAFE.getInt(ADR_row + i);
- if (UNSAFE.getByte(ADR_visited + dest) == 0) {
- dist = cost + getLongestPath(dest);
- if (dist > max) {
- max = dist;
- }
- }
- }
- UNSAFE.putByte(ADR_visited + nodeID, ZERO);
- return max;
- }
- static Unsafe getUnsafe() {
- try {
- Field singleoneInstanceField = Unsafe.class.getDeclaredField("theUnsafe");
- singleoneInstanceField.setAccessible(true);
- return (Unsafe) singleoneInstanceField.get(null);
- } catch (Exception e) {
- return null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement