Advertisement
super_tycoon

lpath java

Dec 20th, 2014
485
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.89 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.FileReader;
  3. import java.util.Arrays;
  4.  
  5. public class jv{
  6.  
  7.     static int numNodes = -1;
  8.     static final int[][] nodes;
  9.     static final boolean[] visited;
  10.  
  11.     static {
  12.         nodes = readPlaces();
  13.         visited = new boolean[numNodes];
  14.     }
  15.  
  16.     public static void main(final String[] args) throws Exception{
  17.         final long start = System.currentTimeMillis();
  18.         final int len = getLongestPath(0);
  19.         final long duration = System.currentTimeMillis() - start;
  20.         System.out.printf("%d LANGUAGE Java %d\n", len, duration);
  21.     }
  22.  
  23.     /**
  24.      * int[node][dest|cost|...]
  25.      */
  26.     static int[][] readPlaces() {
  27.         try (BufferedReader in = new BufferedReader(new FileReader("agraph"))) {
  28.             final String s = in.readLine();
  29.             numNodes = Integer.parseInt(s);
  30.             final int[][] nodes = new int[numNodes][];
  31.             for(int i = 0; i < numNodes; i++){
  32.                 nodes[i]= new int[0];
  33.             }
  34.             while (in.ready()) {
  35.                 final String ln = in.readLine();
  36.                 final String[] nums = ln.split("[ \t]+");
  37.                 if(nums.length != 3){
  38.                     break;
  39.                 }
  40.                 final int node = Integer.parseInt(nums[0]);
  41.                 final int neighbour = Integer.parseInt(nums[1]);
  42.                 final int cost = Integer.parseInt(nums[2]);
  43.  
  44.                 final int index = nodes[node].length;
  45.                 final int[] replacement = Arrays.copyOf(nodes[node], index + 2);
  46.                 replacement[index] = neighbour;
  47.                 replacement[index+1] = cost;
  48.  
  49.                 nodes[node] = replacement;
  50.             }
  51.             return nodes;
  52.         } catch (Exception e) {
  53.             return null;
  54.         }
  55.     }
  56.  
  57.     static int getLongestPath(final int nodeID){
  58.         visited[nodeID] = true;
  59.         int dist, max=0;
  60.  
  61.         final int length = nodes[nodeID].length;
  62.  
  63.         for (int i = 0; i < length; i+=2) {
  64.  
  65.             final int dest = nodes[nodeID][i];
  66.  
  67.             if (!visited[dest]) {
  68.                 dist = nodes[nodeID][i + 1] + getLongestPath(dest);
  69.                 if (dist > max) {
  70.                     max = dist;
  71.                 }
  72.             }
  73.         }
  74.  
  75.         visited[nodeID] = false;
  76.         return max;
  77.     }
  78.  
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement