Advertisement
Alisator

PAL2_1

Oct 18th, 2014
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.39 KB | None | 0 0
  1. package Main;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5.  
  6. /**
  7.  * @author alisa
  8.  */
  9. public class PAL2 {
  10.  
  11.     static public int N; //hotels counts = |vertexes|
  12.     static public int M; //track counts = |edges|
  13.     static public int[][] hotels;
  14.     static public int[][] hotelsHelp;
  15.     static public int indexStart;
  16.     static public int sum = 0;
  17.     static public int indexHelp = 0;
  18.  
  19.     public static void main(String[] args) throws IOException {
  20.  
  21.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  22.         String[] in = br.readLine().split(" ");
  23.         N = Integer.parseInt(in[0]);
  24.         M = Integer.parseInt(in[1]);
  25.         hotels = new int[M][3]; //x y z - uchovame puvodni informaci
  26.         hotelsHelp = new int[M][2]; //x y - pouze prepisujeme vrcholy priznakem
  27.         for (int i = 0; i < M; i++) {
  28.             in = br.readLine().split(" ");
  29.             //x,y,z
  30.             hotels[i][0] = Integer.parseInt(in[0]); //x vertex
  31.             hotels[i][1] = Integer.parseInt(in[1]); //y vertex
  32.             hotels[i][2] = Integer.parseInt(in[2]); //distance between x and y
  33.         }
  34.         //sorted by distance
  35.         hotels = sortArrayByColumn(2);
  36.         int indexConn;
  37.         int length = 0;
  38.         for(int i = 0; i < M; i++)
  39.         {   //hrana jeste nenavstivena a neoznackovana
  40.             if(hotelsHelp[i][0] == 0 && hotelsHelp[i][1] == 0)
  41.             {   //oznackuje vsechny shodne vrcholy
  42.                 setSameVertexes(i);
  43.             }//hrana uz navstivena (oznackovana vzdalenosti)
  44.             else
  45.             {
  46.                 indexStart = i;
  47.                 indexConn = findConnection(i);
  48.                 setSameVertexes(i);
  49.                 length += hotels[indexConn][2];
  50.             }
  51.         }
  52.         for (int i = 0; i < M; i++) {
  53.             for (int j = 0; j < 3; j++) {
  54.                 System.out.print(hotels[i][j] + " ");
  55.             }
  56.             System.out.println("");
  57.         }
  58.         System.out.println(length);
  59.        
  60.     }
  61.     /**
  62.     *najde spojnici, pokud existuje, se kterou aktualni bod tvori vetsi soucet
  63.     * @param index, od ktereho prohledavame vys
  64.     * @return indexHelp = index spojnice, se kterou tvoji aktualni bod vetsi soucet
  65.     */
  66.     public static int findConnection(int index){
  67.         int indexNext = 0;
  68.         for(int i = index-1; index >= 0; i--)
  69.         {   //shoduji-li se priznaky vrcholu
  70.             if(hotelsHelp[indexStart][0] == hotelsHelp[i][0] || hotelsHelp[indexStart][1] == hotelsHelp[i][1])
  71.             {   //first found - ulozime, aby sel vratit
  72.                 indexHelp = i;
  73.                 sum += (hotelsHelp[i][0] + hotels[indexStart][2]);
  74.                 //hledej dalsi spojnici
  75.                 indexNext = findConnection(i);
  76.                 //vetsi soucet nove spojnice, nova suma a hledame dal v dalsim foru
  77.                 if(hotelsHelp[indexNext][0] + hotels[indexStart][2] > sum ){
  78.                    sum = hotelsHelp[indexNext][0] + hotels[indexStart][2];
  79.                    indexHelp = indexNext;
  80.                 }
  81.             }//neshoduji, hledej v podurovnii
  82.             else{
  83.                 indexHelp = findConnection(i);
  84.             }
  85.         }
  86.         //return 0 = zadny spoj, return cislo = nalezeny spoj
  87.     return indexHelp;    
  88.     }
  89.     /**
  90.     *nastavit vsechny stejne vrcholy na stejny priznak
  91.     * @param index, od ktereho nastavujeme niz
  92.     */
  93.     public static void setSameVertexes(int index) {
  94.         for (int i = index + 1; i < M; i++) {
  95.             //najdi vsechny dalsi stejne vrcholy a nastav jim priznak delkz
  96.             int distance = hotels[0][2];
  97.             if (hotels[i][0] == hotels[index][0]) {
  98.                 hotelsHelp[i][0] = distance;
  99.             }
  100.             if (hotels[i][1] == hotels[index][1]) {
  101.                 hotelsHelp[i][1] = distance;
  102.             }
  103.             //nastav priznak delky
  104.             hotelsHelp[index][0] = distance;
  105.             hotelsHelp[index][1] = distance;
  106.         }
  107.     }
  108.  
  109.     public static int[][] sortArrayByColumn(int column) {
  110.         int[][] sortedArray = new int[M][3];
  111.         int min = hotels[0][column];
  112.         int max = hotels[0][column];
  113.         //find min and max values for histogram from current column
  114.         for (int i = 1; i < M; i++) {
  115.             if (hotels[i][column] < min) {
  116.                 min = hotels[i][column];
  117.             } else if (hotels[i][column] > max) {
  118.                 max = hotels[i][column];
  119.             }
  120.         }
  121.         //histogram length from min to max
  122.         int[] hist = new int[max - min + 1];
  123.         for (int i = 0; i < M; i++) {
  124.             hist[hotels[i][column] - min]++;
  125.         }
  126. //        for (int j = 0; j < hist.length; j++) {
  127. //                System.out.print(hist[j] + " ");
  128. //        }
  129.         //pole vyskytu
  130.         hist[0]--;
  131.         for (int i = 1; i < hist.length; i++) {
  132.             hist[i] = hist[i] + hist[i - 1];
  133.         }
  134. //        for (int j = 0; j < hist.length; j++) {
  135. //                System.out.print(hist[j] + " ");
  136. //        }
  137.         //min to max
  138.         for (int i = M - 1; i >= 0; i--) {
  139.             int index = hist[hotels[i][column] - min];
  140.             sortedArray[index][column] = hotels[i][column];
  141.             sortedArray[index][1] = hotels[i][1];
  142.             sortedArray[index][0] = hotels[i][0];
  143.         }
  144.         return sortedArray;
  145.     }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement