Advertisement
Alisator

PAL2_5

Oct 20th, 2014
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.85 KB | None | 0 0
  1. package pal;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6.  
  7. public class Main {
  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.         int lastUsedLength = 0;
  27.         int helpOldLength = 0;
  28.         int indexLastUsedEdge = 0;
  29.         for (int i = 0; i < M; i++) {
  30.             in = br.readLine().split(" ");
  31.             //x,y,z
  32.             hotels[i][0] = Integer.parseInt(in[0]); //x vertex
  33.             hotels[i][1] = Integer.parseInt(in[1]); //y vertex
  34.             hotels[i][2] = Integer.parseInt(in[2]); //distance between x and y
  35.         }
  36.         //sorted by distance
  37.         hotels = sortArrayByColumn(2);
  38.  
  39.         for (int i = 0; i < M; i++) {
  40.             //nova delka jina nez posledni
  41.             if (hotels[i][2] != lastUsedLength) {
  42.                 //s                System.out.println("jina delka " + hotels[i][1] + " " +  hotels[i][0]);
  43.                 helpOldLength = length[hotels[i][0]];
  44.                 if (helpOldLength < (length[hotels[i][1]] + hotels[i][2])) {
  45.                     lastUsedLength = hotels[i][2];
  46.                     length[hotels[i][0]] = length[hotels[i][1]] + hotels[i][2];
  47.  
  48.                     if (length[hotels[i][0]] > max) {
  49.                         max = length[hotels[i][0]];
  50.                     }
  51.                 }
  52.                 if (length[hotels[i][1]] < (helpOldLength + hotels[i][2])) {
  53.                     lastUsedLength = hotels[i][2];
  54.                     length[hotels[i][1]] = helpOldLength + hotels[i][2];
  55.                     if (length[hotels[i][1]] > max) {
  56.                         max = length[hotels[i][1]];
  57.                     }
  58.                 }
  59.             } else { //stejna delka jako posledni
  60.                 //posledni
  61.                 indexLastUsedEdge = i - 1;
  62.                 //hrana jeste nepouzita - lze pricist
  63.                 if (length[hotels[i][1]] == 0 && length[hotels[i][0]] == 0) {
  64.                     //     System.out.println("stejna dalka, hrana nepouzita " + hotels[i][1] + " " +  hotels[i][0]);
  65.  
  66.                     helpOldLength = length[hotels[i][0]];
  67.                     if (helpOldLength < (length[hotels[i][1]] + hotels[i][2])) {
  68.                         length[hotels[i][0]] = length[hotels[i][1]] + hotels[i][2];
  69.  
  70.                         if (length[hotels[i][0]] > max) {
  71.                             max = length[hotels[i][0]];
  72.                         }
  73.                     }
  74.                     if (length[hotels[i][1]] < (helpOldLength + hotels[i][2])) {
  75.                         length[hotels[i][1]] = helpOldLength + hotels[i][2];
  76.                         if (length[hotels[i][1]] > max) {
  77.                             max = length[hotels[i][1]];
  78.                         }
  79.                     }
  80.                     //    System.out.println(indexLastUsedEdge);
  81.                     indexLastUsedEdge = i;
  82.                     continue;
  83.                 }
  84.                 //hrana se stejnou delkou nesousedi s posledni hranou se stejnou delkou
  85.                 if ((hotels[i][1] != hotels[indexLastUsedEdge][1] && hotels[i][1] != hotels[indexLastUsedEdge][0])
  86.                         && (hotels[i][0] != hotels[indexLastUsedEdge][1] && hotels[i][0] != hotels[indexLastUsedEdge][0])) {
  87.                     //          System.out.println("stejna dalka, hrana pouzita " + hotels[i][1] + " " +  hotels[i][0]);
  88.  
  89.                     helpOldLength = length[hotels[i][0]];
  90.                     if (helpOldLength < (length[hotels[i][1]] + hotels[i][2])) {
  91.                         length[hotels[i][0]] = length[hotels[i][1]] + hotels[i][2];
  92.  
  93.                         if (length[hotels[i][0]] > max) {
  94.                             max = length[hotels[i][0]];
  95.                         }
  96.                     }
  97.                     if (length[hotels[i][1]] < (helpOldLength + hotels[i][2])) {
  98.                         length[hotels[i][1]] = helpOldLength + hotels[i][2];
  99.                         if (length[hotels[i][1]] > max) {
  100.                             max = length[hotels[i][1]];
  101.                         }
  102.                     }
  103.                     //       System.out.println(indexLastUsedEdge);
  104.                     indexLastUsedEdge = i;
  105.                 } else { //sousedi a jeden vrchol nevahovan
  106.                     helpOldLength = length[hotels[i][1]];
  107.                     if (length[hotels[i][1]] == 0) {
  108.                             length[hotels[i][1]] = helpOldLength + hotels[i][2];
  109.                             if (length[hotels[i][1]] > max) {
  110.                                 max = length[hotels[i][1]];
  111.                             }
  112.                     }
  113.                     if (length[hotels[i][0]] == 0) {
  114.                        
  115.                             length[hotels[i][0]] = length[hotels[i][1]] + hotels[i][2];
  116.  
  117.                             if (length[hotels[i][0]] > max) {
  118.                                 max = length[hotels[i][0]];
  119.                             }
  120.                     }
  121.                     indexLastUsedEdge = i;
  122.                 }
  123.             }
  124.         }
  125.  
  126.         System.out.println(max);
  127.     }
  128.  
  129.     public static int[][] sortArrayByColumn(int column) {
  130.         int[][] sortedArray = new int[M][3];
  131.         int min = hotels[0][column];
  132.         int max = hotels[0][column];
  133.         //find min and max values for histogram from current column
  134.         for (int i = 1; i < M; i++) {
  135.             if (hotels[i][column] < min) {
  136.                 min = hotels[i][column];
  137.             } else if (hotels[i][column] > max) {
  138.                 max = hotels[i][column];
  139.             }
  140.         }
  141.         //histogram length from min to max
  142.         int[] hist = new int[max - min + 1];
  143.         for (int i = 0; i < M; i++) {
  144.             hist[hotels[i][column] - min]++;
  145.         }
  146.  
  147.         //pole vyskytu
  148.         hist[0]--;
  149.         for (int i = 1; i < hist.length; i++) {
  150.             hist[i] = hist[i] + hist[i - 1];
  151.         }
  152.  
  153.         //min to max
  154.         for (int i = M - 1; i >= 0; i--) {
  155.             int histIndex = hotels[i][column] - min;
  156.             int index = hist[histIndex];
  157.             sortedArray[index][column] = hotels[i][column];
  158.             sortedArray[index][1] = hotels[i][1];
  159.             sortedArray[index][0] = hotels[i][0];
  160.             hist[histIndex]--;
  161.         }
  162.         return sortedArray;
  163.     }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement