Advertisement
Alisator

Dejvos

Nov 4th, 2014
139
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 Queue dijkstraQ; //fronta pro uchovavani navstivenych vrcholu pro Dijkstru
  27.     //reprezentace grafu
  28.     public static HashMap<Integer, Integer> edgeEnd; //vrchol V a delka hrany
  29.     public static HashMap[] vertices; //vrchol U odpovida indexu pole
  30.     public static HashMap<Integer, Integer> edgeEndGas;
  31.     public static HashMap[] gas;
  32.  
  33.     public static void main(String[] args) throws IOException {
  34.  
  35.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  36.         String[] in = br.readLine().split(" ");
  37.         M = Integer.parseInt(in[0]);
  38.         N = Integer.parseInt(in[1]);
  39.         H = Integer.parseInt(in[2]);
  40.         F = Integer.parseInt(in[3]);
  41.         S = Integer.parseInt(in[4]);
  42.         T = Integer.parseInt(in[5]);
  43.         countOfCities = M + N;
  44. //        highways = new int[countOfCities][countOfCities]; //matice
  45.         edgeEnd = new HashMap<>();
  46.         vertices = new HashMap[countOfCities];
  47.         citiesHasGas = new boolean[countOfCities];
  48.         for (int i = 0; i < M; i++) {
  49.             int inM = Integer.parseInt(br.readLine());
  50.             citiesHasGas[inM] = true; //mesto je pumpa
  51.         }
  52.         edgeEndGas = new HashMap<>();
  53.         gas = new HashMap[M];
  54.         //mozna udelat for na vertices[x] = new HashMap<>(); a gas = new... jinak null pointer?
  55.  
  56.         for (int i = 0; i < countOfCities;) {
  57.             vertices[i] = new HashMap<>();
  58.             gas[i] = new HashMap<>();
  59.         }
  60.         for (int i = 0; i < H; i++) {
  61.             String[] inH = br.readLine().split(" ");
  62.             int x = Integer.parseInt(inH[0]); //x
  63.             int y = Integer.parseInt(inH[1]); //y
  64.             int l = Integer.parseInt(inH[2]);
  65.             vertices[x].put(y, l);
  66.         }
  67.         //  DijkstraCreateNewGraph(S);
  68.         //int lengthEnd = Dijkstra(S);
  69.         //System.out.println(lengthEnd);
  70.     }
  71.  
  72.     public static void DijkstraCreateNewGraph(int from) {
  73.         //       int[] prev = new int[countOfCities]; //nepotřebujem cestu, jen vahy, vzdalenosti
  74.         dijkstraQ = new PriorityQueue();
  75.         citiesWeight = new int[countOfCities]; //index odpovida label vrcholu, weight
  76.         citiesVisited = new boolean[countOfCities];
  77.         int counIndexOfpump = 0;
  78.         for (int i = 0; i < countOfCities; i++) {
  79.             if (!citiesHasGas[i]) {
  80.                 continue;
  81.             } else {
  82.                 //zaciname jen z pump
  83.                 from = i;
  84.                 counIndexOfpump++;
  85.             }
  86.             for (int j = 0; j < countOfCities; j++) {
  87.                 // if (i != from) {//zbytecne kontrola ifu, nastavim start zvlast
  88.                 citiesWeight[j] = Integer.MAX_VALUE;
  89.             }
  90.             citiesWeight[from] = 0;
  91.             dijkstraQ.add(from);
  92.  
  93.             while (!dijkstraQ.isEmpty()) {
  94.                 //poll retrieves and remove element... u priority the lowest one
  95.                 int u = (int) dijkstraQ.poll();
  96.                 citiesVisited[u] = true; //visited
  97.                 //
  98.                 dijkstraQ.remove(u);
  99.                 // if(citiesWeight[u] == Integer.MAX) break; //nedostupny vrchol
  100.                 //pro kazdyho souseda v vrcholu u
  101.                 Set<Integer> keys = vertices[u].keySet();
  102.                 for (Integer v : keys) {//jestli soused v nebyl navstiven
  103.                     if (citiesVisited[v] == false) {//spocitej help = citiesWeight[u] + delka mezi uv
  104.                         int length = citiesWeight[u] + (int) vertices[u].get(v);
  105.                         if (length < citiesWeight[v] && length <= F) {   //nastav novou dvahu pro v a jeho predka, pokud staci delka paliva na cestu mezi pumpama
  106.                             // u a v pumpy, pridej je do noveho redukovaneho grafu
  107.                             gas[counIndexOfpump].put(v, length);
  108.                             dijkstraQ.remove(v);
  109. //                            citiesWeight[v] = length;
  110.                             //prev[v] = u;                        
  111.                             dijkstraQ.add(v);
  112.                         }
  113.                     }
  114.                 }
  115.             }
  116.         }
  117.     }
  118.  
  119.     public static int Dijkstra(int from) {
  120.         //       int[] prev = new int[countOfCities]; //nepotřebujem cestu, jen vahy, vzdalenosti
  121.         //vsechny promenne znova instancujeme
  122.         dijkstraQ = new PriorityQueue();
  123.         //vetsi pole, ale index odovida labelu vrcholu... jinak bych musela v tom poli hledat nejak, tak nevim, co s tim?
  124.         citiesWeight = new int[countOfCities]; //index odpovida label vrcholu, weight
  125.         citiesVisited = new boolean[countOfCities];
  126.         dijkstraQ.add(from);
  127.  
  128.         for (int j = 0; j < countOfCities; j++) {
  129.             // if (i != from) {//zbytecne kontrola ifu, nastavim start zvlast
  130.  
  131.             citiesWeight[j] = Integer.MAX_VALUE;
  132.         }
  133.         citiesWeight[from] = 0;
  134.  
  135.         while (!dijkstraQ.isEmpty()) {
  136.             int u = (int) dijkstraQ.poll();
  137.             citiesVisited[u] = true; //visited
  138.             //
  139.             dijkstraQ.remove(u);
  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