Advertisement
Alisator

PAL2_bez stejnych hran

Oct 20th, 2014
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.14 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 nebo jeste nepouzita hrana
  41.             if ((hotels[i][2] != lastUsedLength )|| (length[hotels[i][1]] == 0 && length[hotels[i][0]] == 0)) {
  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.              
  61.                 //hrana se stejnou delkou nesousedi s posledni hranou se stejnou delkou
  62.                 if ((hotels[i][1] != hotels[indexLastUsedEdge][1] && hotels[i][1] != hotels[indexLastUsedEdge][0])
  63.                         && (hotels[i][0] != hotels[indexLastUsedEdge][1] && hotels[i][0] != hotels[indexLastUsedEdge][0])) {
  64.                     //          System.out.println("stejna dalka, hrana pouzita " + hotels[i][1] + " " +  hotels[i][0]);
  65.                 }
  66.             }
  67.         }
  68.         System.out.println(max);
  69.     }
  70.  
  71.     public static int[][] sortArrayByColumn(int column) {
  72.         int[][] sortedArray = new int[M][3];
  73.         int min = hotels[0][column];
  74.         int max = hotels[0][column];
  75.         //find min and max values for histogram from current column
  76.         for (int i = 1; i < M; i++) {
  77.             if (hotels[i][column] < min) {
  78.                 min = hotels[i][column];
  79.             } else if (hotels[i][column] > max) {
  80.                 max = hotels[i][column];
  81.             }
  82.         }
  83.         //histogram length from min to max
  84.         int[] hist = new int[max - min + 1];
  85.         for (int i = 0; i < M; i++) {
  86.             hist[hotels[i][column] - min]++;
  87.         }
  88.         //pole vyskytu
  89.         hist[0]--;
  90.         for (int i = 1; i < hist.length; i++) {
  91.             hist[i] = hist[i] + hist[i - 1];
  92.         }
  93.         //min to max
  94.         for (int i = M - 1; i >= 0; i--) {
  95.             int histIndex = hotels[i][column] - min;
  96.             int index = hist[histIndex];
  97.             sortedArray[index][column] = hotels[i][column];
  98.             sortedArray[index][1] = hotels[i][1];
  99.             sortedArray[index][0] = hotels[i][0];
  100.             hist[histIndex]--;
  101.         }
  102.         return sortedArray;
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement