Advertisement
Alisator

PAL3_1v

Oct 31st, 2014
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.39 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.  
  47.         edgeEnd = new HashMap<>();
  48.         vertices = new HashMap[countOfCities];
  49.         citiesHasGas = new boolean[countOfCities];
  50.         for (int i = 0; i < M; i++) {
  51.             int inM = Integer.parseInt(br.readLine());
  52.             citiesHasGas[inM] = true; //mesto je pumpa
  53.             countOfGas++;
  54.         }
  55.         edgeEndGas = new HashMap<>();
  56.         gas = new HashMap[countOfGas];
  57.         //mozna udelat for na vertices[x] = new HashMap<>(); a gas = new...
  58.         for (int i = 0; i < H; i++) {
  59.             String[] inH = br.readLine().split(" ");
  60.             int x = Integer.parseInt(inH[0]); //x
  61.             int y = Integer.parseInt(inH[1]); //y
  62.             int l = Integer.parseInt(inH[2]);
  63.             //Integer cannot be converted to HashMap Type
  64.             if (vertices[x] == null) {
  65.                 vertices[x] = new HashMap<>();
  66.             }
  67.             vertices[x].put(y, l);
  68.         }
  69.         DijkstraCreateNewGraph(0);
  70.         int lengthEnd = Dijkstra(0);
  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.         dijkstraQ.add(from);
  80.         citiesWeight[from] = 0;
  81.         int fuel = F;
  82.         for (int i = 0; i < countOfCities; i++) {
  83.             if (i != from) {
  84.                 citiesWeight[i] = Integer.MAX_VALUE;
  85.             }
  86.         }
  87.         while (!dijkstraQ.isEmpty()) {
  88.             int u = (int) dijkstraQ.poll();
  89.             citiesVisited[u] = true; //visited
  90.             // if(citiesWeight[u] == Integer.MAX) break; //nedostupny vrchol
  91.             //pro kazdyho souseda v vrcholu u
  92.             Set<Integer> keys = vertices[u].keySet();
  93.             for (Integer v : keys) {//jestli soused v nebyl navstiven
  94.                 if (citiesVisited[v] == false) {//spocitej help = citiesWeight[u] + delka mezi uv
  95.                     int length = citiesWeight[u] + (int) vertices[u].get(v);
  96.                     if (length < citiesWeight[v] && length < fuel) {   //nastav novou dvahu pro v a jeho predka
  97.                         if (!citiesHasGas[v]) {
  98.                             //neni pumpa, nenabije se, ale odecte se
  99.                             fuel -= length;
  100.                         } else {//je pumpa, naplni se
  101.                             fuel = F;
  102.                             //if u a v pumpy, pridej je do noveho redukovaneho grafu
  103.                             if (citiesHasGas[u]) {
  104.                                 if (vertices[u] == null) {
  105.                                     vertices[u] = new HashMap<>();
  106.                                 }
  107.                                 vertices[u].put(v, length);
  108.                             }
  109.                         }
  110.                         dijkstraQ.remove(v);
  111.                         citiesWeight[v] = length;
  112.                         //prev[v] = u;                        
  113.                         dijkstraQ.add(v);
  114.                     }
  115.                 }
  116.             }
  117.         }
  118.     }
  119.  
  120.     public static int Dijkstra(int from) {
  121.         //       int[] prev = new int[countOfCities]; //nepotřebujem cestu, jen vahy, vzdalenosti
  122.         dijkstraQ = new PriorityQueue();
  123.         citiesWeight = new int[countOfGas]; //index odpovida label vrcholu, weight
  124.         citiesVisited = new boolean[countOfCities];
  125.         dijkstraQ.add(from);
  126.         citiesWeight[from] = 0;
  127.         for (int i = 0; i < countOfGas; i++) {
  128.             if (i != from) {
  129.                 citiesWeight[i] = Integer.MAX_VALUE;
  130.             }
  131.         }
  132.         while (!dijkstraQ.isEmpty()) {
  133.             int u = (int) dijkstraQ.poll();
  134.             citiesVisited[u] = true; //visited
  135.             // if(citiesWeight[u] == Integer.MAX) break; //nedostupny vrchol
  136.             //pro kazdyho souseda v vrcholu u
  137.             Set<Integer> keys = gas[u].keySet();
  138.             for (Integer v : keys) {//jestli soused v nebyl navstiven
  139.                 if (citiesVisited[v] == false) {//spocitej help = citiesWeight[u] + delka mezi uv
  140.                     int length = citiesWeight[u] + (int) gas[u].get(v);
  141.                     if (length < citiesWeight[v]) {   //nastav novou dvahu pro v a jeho predka
  142.                         dijkstraQ.remove(v);
  143.                         citiesWeight[v] = length;
  144.                         //prev[v] = u;
  145.                         dijkstraQ.add(v);
  146.                     }
  147.                 }
  148.             }
  149.         }
  150.         return citiesWeight[T];
  151.     }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement