Advertisement
Alisator

PAL2_2

Oct 19th, 2014
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.16 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package Main;
  7.  
  8. import java.io.BufferedReader;
  9. import java.io.IOException;
  10. import java.io.InputStreamReader;
  11.  
  12. /**
  13.  *
  14.  * @author martin
  15.  */
  16. public class PAL2 {
  17.  
  18.     static public int N; //hotels counts = |vertexes|
  19.     static public int M; //track counts = |edges|
  20.     static public int[][] hotels;
  21.     static public int[][] hotelsHelp;
  22.     static public int indexConnection = 0;
  23.     static public boolean notConn = false;
  24.  
  25.  
  26.     /**
  27.      * @param args the command line arguments
  28.      * @throws java.io.IOException
  29.      */
  30.     public static void main(String[] args) throws IOException {
  31.  
  32.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  33.         String[] in = br.readLine().split(" ");
  34.         N = Integer.parseInt(in[0]);
  35.         M = Integer.parseInt(in[1]);
  36.         hotels = new int[M][3];
  37.         hotelsHelp = new int[M][3];
  38.         for (int i = 0; i < M; i++) {
  39.             in = br.readLine().split(" ");
  40.             //x,y,z
  41.             hotels[i][0] = Integer.parseInt(in[0]); //x vertex
  42.             hotels[i][1] = Integer.parseInt(in[1]); //y vertex
  43.             hotels[i][2] = Integer.parseInt(in[2]); //distance between x and y
  44.             hotelsHelp[i][2] = Integer.parseInt(in[2]) ;
  45.         }
  46.         //sorted by distance
  47.         hotels = sortArrayByColumn(2);
  48.  
  49.         for (int i = M - 1; i >= 0; i--) {
  50.             //nenavstiveno
  51.             if (hotelsHelp[i][0] == 0 && hotelsHelp[i][1] == 0) {//nastavime priznak prislusnym vrcholum
  52.                 setSameVertexes(i);
  53.             } else {//nastavime priznak prislusnym vrcholum
  54.                 addVisitedVertexes(i);
  55.             }
  56.             for (int k = M - 1; k >= 0; k--) {
  57.                 for (int l = 0; l < 3; l++) {
  58.                     System.out.print(hotelsHelp[k][l] + " ");
  59.                 }
  60.                System.out.println("") ;
  61.             }
  62.             System.out.println("xxx");
  63.         }
  64.  
  65.     }
  66.  
  67.     public static void addVisitedVertexes(int currentVertex) {
  68.         if((hotels[currentVertex][2] + hotelsHelp[currentVertex][0]) > (hotels[currentVertex][2] + hotelsHelp[currentVertex][1]))
  69.         {
  70.             System.out.println(hotels[currentVertex][2] + " " + hotelsHelp[currentVertex][0]);
  71.             hotelsHelp[currentVertex][1] = (hotels[currentVertex][2] + hotelsHelp[currentVertex][0]);
  72.             hotelsHelp[currentVertex][0] = (hotels[currentVertex][2] + hotelsHelp[currentVertex][0]);
  73.             hotelsHelp[currentVertex][2] = (hotels[currentVertex][2] + hotelsHelp[currentVertex][0]);
  74.         }
  75.         if((hotels[currentVertex][2] + hotelsHelp[currentVertex][0]) < (hotels[currentVertex][2] + hotelsHelp[currentVertex][1]))
  76.         {
  77.             System.out.println(hotels[currentVertex][2] + " " + hotelsHelp[currentVertex][0]);
  78.             hotelsHelp[currentVertex][1] = (hotels[currentVertex][2] + hotelsHelp[currentVertex][1]);
  79.             hotelsHelp[currentVertex][0] = (hotels[currentVertex][2] + hotelsHelp[currentVertex][1]);
  80.             hotelsHelp[currentVertex][2] = (hotels[currentVertex][2] + hotelsHelp[currentVertex][1]);
  81.         }
  82.         setSameVertexes(currentVertex);
  83.     }
  84.  
  85.     public static void setSameVertexes(int currentVertex) {
  86.         for (int i = currentVertex; i >= 0; i--) {
  87.             //najdi vsechny dalsi stejne vrcholy a nastav jim nove delky
  88.             int distance = hotelsHelp[currentVertex][2];
  89.             if (hotels[i][0] == hotels[currentVertex][0]|| hotels[i][0] == hotels[currentVertex][1]) {
  90.                 hotelsHelp[i][0] = distance;
  91.             }
  92.             if (hotels[i][1] == hotels[currentVertex][1] || hotels[i][1] == hotels[currentVertex][0]) {
  93.                 hotelsHelp[i][1] = distance;
  94.             }
  95.         }
  96.     }
  97.  
  98.     public static int[][] sortArrayByColumn(int column) {
  99.         int[][] sortedArray = new int[M][3];
  100.         int min = hotels[0][column];
  101.         int max = hotels[0][column];
  102.         //find min and max values for histogram from current column
  103.         for (int i = 1; i < M; i++) {
  104.             if (hotels[i][column] < min) {
  105.                 min = hotels[i][column];
  106.             } else if (hotels[i][column] > max) {
  107.                 max = hotels[i][column];
  108.             }
  109.         }
  110.         //histogram length from min to max
  111.         int[] hist = new int[max - min + 1];
  112.         for (int i = 0; i < M; i++) {
  113.             hist[hotels[i][column] - min]++;
  114.         }
  115.  
  116.         //pole vyskytu
  117.         hist[0]--;
  118.         for (int i = 1; i < hist.length; i++) {
  119.             hist[i] = hist[i] + hist[i - 1];
  120.         }
  121.  
  122.         //min to max
  123.         for (int i = M - 1; i >= 0; i--) {
  124.             int histIndex = hotels[i][column] - min;
  125.             int index = hist[histIndex];
  126.             sortedArray[index][column] = hotels[i][column];
  127.             hotelsHelp[index][column] = hotels[i][column];
  128.             sortedArray[index][1] = hotels[i][1];
  129.             sortedArray[index][0] = hotels[i][0];
  130.             hist[histIndex]--;
  131.         }
  132.         return sortedArray;
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement