Advertisement
Alisator

PAL2_3

Oct 19th, 2014
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.02 KB | None | 0 0
  1. package Main;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6.  
  7. public class PAL2 {
  8.  
  9.     static public int N; //hotels counts = |vertexes|
  10.     static public int M; //track counts = |edges|
  11.     static public int[][] hotels;
  12.     static public int[][] hotelsHelp;
  13.     static public int indexConnection = 0;
  14.     static public boolean notConn = false;
  15.     static public int[] length;
  16.  
  17.     public static void main(String[] args) throws IOException {
  18.  
  19.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  20.         String[] in = br.readLine().split(" ");
  21.         N = Integer.parseInt(in[0]);
  22.         M = Integer.parseInt(in[1]);
  23.         length = new int[N];
  24.         hotels = new int[M][3];
  25.         int max = 0;
  26.         for (int i = 0; i < M; i++) {
  27.             in = br.readLine().split(" ");
  28.             //x,y,z
  29.             hotels[i][0] = Integer.parseInt(in[0]); //x vertex
  30.             hotels[i][1] = Integer.parseInt(in[1]); //y vertex
  31.             hotels[i][2] = Integer.parseInt(in[2]); //distance between x and y
  32.         }
  33.         //sorted by distance
  34.         hotels = sortArrayByColumn(2);
  35.  
  36.         for (int i = 0; i < M; i++) {
  37.             int helpOldLength = length[hotels[i][0]];
  38.             if(helpOldLength < (length[hotels[i][1]] + hotels[i][2] ))
  39.             {
  40.                 length[hotels[i][0]] = length[hotels[i][1]] + hotels[i][2];
  41.                 if(length[hotels[i][0]] > max) {max = length[hotels[i][0]];}
  42.             }
  43.             if(length[hotels[i][1]] < (helpOldLength + hotels[i][2] ))
  44.             {
  45.                 length[hotels[i][1]] = helpOldLength + hotels[i][2];
  46.                 if(length[hotels[i][1]] > max) {max = length[hotels[i][1]];}
  47.             }
  48.         }
  49.         System.out.println(max);
  50.     }
  51.  
  52.       public static int[][] sortArrayByColumn(int column) {
  53.         int[][] sortedArray = new int[M][3];
  54.         int min = hotels[0][column];
  55.         int max = hotels[0][column];
  56.         //find min and max values for histogram from current column
  57.         for (int i = 1; i < M; i++) {
  58.             if (hotels[i][column] < min) {
  59.                 min = hotels[i][column];
  60.             } else if (hotels[i][column] > max) {
  61.                 max = hotels[i][column];
  62.             }
  63.         }
  64.         //histogram length from min to max
  65.         int[] hist = new int[max - min + 1];
  66.         for (int i = 0; i < M; i++) {
  67.             hist[hotels[i][column] - min]++;
  68.         }
  69.  
  70.         //pole vyskytu
  71.         hist[0]--;
  72.         for (int i = 1; i < hist.length; i++) {
  73.             hist[i] = hist[i] + hist[i - 1];
  74.         }
  75.  
  76.         //min to max
  77.         for (int i = M - 1; i >= 0; i--) {
  78.             int histIndex = hotels[i][column] - min;
  79.             int index = hist[histIndex];
  80.             sortedArray[index][column] = hotels[i][column];
  81.             sortedArray[index][1] = hotels[i][1];
  82.             sortedArray[index][0] = hotels[i][0];
  83.             hist[histIndex]--;
  84.         }
  85.         return sortedArray;
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement