Advertisement
Alisator

davidooos

Nov 3rd, 2014
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.67 KB | None | 0 0
  1. package pal;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.HashMap;
  7. import java.util.PriorityQueue;
  8. import java.util.Queue;
  9. import java.util.Set;
  10.  
  11. public class Main {
  12.  
  13.     //read
  14.     static public int M; //countOfCities with gas station
  15.     static public int N; //countOfCities without gas station
  16.     static public int H; //highways
  17.     static public int F; //max distance without refueling
  18.     static public int S; //start label
  19.     static public int T; //end label
  20.     //create
  21. //    static public int[][] highways; //matice sousednosti
  22.     static public int[] citiesWeight; //index odpovida label hotelu - weight for dijkstra
  23.     static public boolean[] citiesVisited; // for dijkstra
  24.     static public boolean[] citiesHasGas;
  25.     static public int countOfCities;
  26.     static public int countOfGas;
  27.     static public Queue dijkstraQ; //fronta pro uchovavani navstivenych vrcholu pro Dijkstru
  28.     //reprezentace grafu
  29.     public static HashMap<Integer, Integer> edgeEnd; //vrchol V a delka hrany
  30.     public static HashMap[] vertices; //vrchol U odpovida indexu pole
  31.     public static HashMap<Integer, Integer> edgeEndGas;
  32.     public static HashMap[] gas;
  33.  
  34.     public static void main(String[] args) throws IOException {
  35.  
  36.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  37.         String[] in = br.readLine().split(" ");
  38.         M = Integer.parseInt(in[0]);
  39.         N = Integer.parseInt(in[1]);
  40.         H = Integer.parseInt(in[2]);
  41.         F = Integer.parseInt(in[3]);
  42.         S = Integer.parseInt(in[4]);
  43.         T = Integer.parseInt(in[5]);
  44.         countOfCities = M + N;
  45. //        highways = new int[countOfCities][countOfCities]; //matice
  46.         edgeEnd = new HashMap<>();
  47.         vertices = new HashMap[countOfCities];
  48.         citiesHasGas = new boolean[countOfCities];
  49.         for (int i = 0; i < M; i++) {
  50.             int inM = Integer.parseInt(br.readLine());
  51.             citiesHasGas[inM] = true; //mesto je pumpa
  52.             countOfGas++;
  53.         }
  54.         edgeEndGas = new HashMap<>();
  55.         gas = new HashMap[countOfGas];
  56.        //JINAK NULL POITER EXCEPTION
  57.      for(int i = 0; i < countOfCities;)
  58.         {
  59.             vertices[i] = new HashMap<>();
  60.             gas[i] = new HashMap<>();
  61.         }
  62.         for (int i = 0; i < H; i++) {
  63.             String[] inH = br.readLine().split(" ");
  64.             int x = Integer.parseInt(inH[0]); //x
  65.             int y = Integer.parseInt(inH[1]); //y
  66.             int l = Integer.parseInt(inH[2]);
  67.             vertices[x].put(y, l);
  68.         }
  69.       //  DijkstraCreateNewGraph(S);
  70.         //int lengthEnd = Dijkstra(S);
  71.         //System.out.println(lengthEnd);
  72.     }
  73.  
  74.     public static void DijkstraCreateNewGraph(int from) {
  75.         //       int[] prev = new int[countOfCities]; //nepotřebujem cestu, jen vahy, vzdalenosti
  76.         dijkstraQ = new PriorityQueue();
  77.         citiesWeight = new int[countOfCities]; //index odpovida label vrcholu, weight
  78.         citiesVisited = new boolean[countOfCities];
  79.         int counIndexOfpump = 0;
  80.         for (int i = 0; i < countOfCities; i++) {
  81.             if (!citiesHasGas[i]) {
  82.                 continue;
  83.             } else {
  84.                 //zaciname jen z pump
  85.                 from = i;
  86.                 counIndexOfpump++;
  87.             }
  88.             for (int j = 0; j < countOfCities; j++) {
  89.                 // if (i != from) {//zbytecne kontrola ifu, nastavim start zvlast
  90.                 citiesWeight[j] = Integer.MAX_VALUE;
  91.             }
  92.             citiesWeight[from] = 0;
  93.             dijkstraQ.add(from);
  94.             citiesWeight[from] = 0;
  95.  
  96.             while (!dijkstraQ.isEmpty()) {
  97.                 //poll retrieves and remove element... u priority the lowest one
  98.                 int u = (int) dijkstraQ.poll();
  99.                 citiesVisited[u] = true; //visited
  100.                 // if(citiesWeight[u] == Integer.MAX) break; //nedostupny vrchol
  101.                 //pro kazdyho souseda v vrcholu u
  102.                 Set<Integer> keys = vertices[u].keySet();
  103.                 for (Integer v : keys) {//jestli soused v nebyl navstiven
  104.                     if (citiesVisited[v] == false) {//spocitej help = citiesWeight[u] + delka mezi uv
  105.                         int length = citiesWeight[u] + (int) vertices[u].get(v);
  106.                         if (length < citiesWeight[v] && length <= F) {   //nastav novou dvahu pro v a jeho predka, pokud staci delka paliva na cestu mezi pumpama
  107.                             // u a v pumpy, pridej je do noveho redukovaneho grafu
  108. //                            if (gas[counIndexOfpump] == null) {
  109. //                                gas[counIndexOfpump] = new HashMap<>();
  110. //                            }
  111. //                            gas[counIndexOfpump].put(v, length);
  112.                             dijkstraQ.remove(v);
  113.                             citiesWeight[v] = length;
  114.                             //prev[v] = u;                        
  115.                             dijkstraQ.add(v);
  116.                         }
  117.                     }
  118.                 }
  119.             }
  120.         }
  121.     }
  122.  
  123.     public static int Dijkstra(int from) {
  124.         //       int[] prev = new int[countOfCities]; //nepotřebujem cestu, jen vahy, vzdalenosti
  125.         //vsechny promenne znova instancujeme
  126.         dijkstraQ = new PriorityQueue();
  127.         citiesWeight = new int[countOfCities]; //index odpovida label vrcholu, weight
  128.         citiesVisited = new boolean[countOfCities];
  129.         dijkstraQ.add(from);
  130.         citiesWeight[from] = 0;
  131.         for (int j = 0; j < countOfCities; j++) {
  132.             // if (i != from) {//zbytecne kontrola ifu, nastavim start zvlast
  133.             citiesWeight[j] = Integer.MAX_VALUE;
  134.         }
  135.         citiesWeight[from] = 0;
  136.  
  137.         while (!dijkstraQ.isEmpty()) {
  138.             int u = (int) dijkstraQ.poll();
  139.             citiesVisited[u] = true; //visited
  140.             // if(citiesWeight[u] == Integer.MAX) break; //nedostupny vrchol
  141.             //pro kazdyho souseda v vrcholu u
  142.             Set<Integer> keys = gas[u].keySet();
  143.             for (Integer v : keys) {//jestli soused v nebyl navstiven
  144.                 if (citiesVisited[v] == false) {//spocitej help = citiesWeight[u] + delka mezi uv
  145.                     int length = citiesWeight[u] + (int) gas[u].get(v);
  146.                     if (length < citiesWeight[v]) {   //nastav novou dvahu pro v a jeho predka
  147.                         dijkstraQ.remove(v);
  148.                         citiesWeight[v] = length;
  149.                         //prev[v] = u;
  150.                         dijkstraQ.add(v);
  151.                     }
  152.                 }
  153.             }
  154.         }
  155.         return citiesWeight[T];
  156.     }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement