Advertisement
Alisator

PAL3-2-11

Nov 2nd, 2014
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.81 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(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.  
  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.            
  88.              if (!citiesHasGas[i]) {
  89.                 continue;
  90.             } else {
  91.                 //zaciname jen z pump
  92.                 from = i;
  93.                 counIndexOfpump++;
  94.             }
  95.             dijkstraQ.add(from);
  96.             citiesWeight[from] = 0;
  97.  
  98.             while (!dijkstraQ.isEmpty()) {
  99.                 int u = (int) dijkstraQ.poll();
  100.                 citiesVisited[u] = true; //visited
  101.                 // if(citiesWeight[u] == Integer.MAX) break; //nedostupny vrchol
  102.                 //pro kazdyho souseda v vrcholu u
  103.                 Set<Integer> keys = vertices[u].keySet();
  104.                 for (Integer v : keys) {//jestli soused v nebyl navstiven
  105.                     if (citiesVisited[v] == false) {//spocitej help = citiesWeight[u] + delka mezi uv
  106.                         int length = citiesWeight[u] + (int) vertices[u].get(v);
  107.                         if (length < citiesWeight[v] && length <= fuel) {   //nastav novou dvahu pro v a jeho predka
  108.                             if (!citiesHasGas[v]) {
  109.                                 //neni pumpa, nenabije se, ale odecte se
  110.                                 fuel -= length;
  111.                             } else {//je pumpa, naplni se
  112.                                 fuel = F;
  113.                                 //if u a v pumpy, pridej je do noveho redukovaneho grafu
  114.                                 if (citiesHasGas[u]) {
  115.                                     if (gas[counIndexOfpump] == null) {
  116.                                         gas[counIndexOfpump] = new HashMap<>();
  117.                                     }
  118.                                     gas[counIndexOfpump].put(v, length);
  119.                                 }
  120.                             }
  121.                             dijkstraQ.remove(v);
  122.                             citiesWeight[v] = length;
  123.                             //prev[v] = u;                        
  124.                             dijkstraQ.add(v);
  125.                         }
  126.                     }
  127.                 }
  128.             }
  129.         }
  130.     }
  131.  
  132.     public static int Dijkstra(int from) {
  133.         //       int[] prev = new int[countOfCities]; //nepotřebujem cestu, jen vahy, vzdalenosti
  134.         dijkstraQ = new PriorityQueue();
  135.         citiesWeight = new int[countOfGas]; //index odpovida label vrcholu, weight
  136.         citiesVisited = new boolean[countOfCities];
  137.         dijkstraQ.add(from);
  138.         citiesWeight[from] = 0;
  139.         for (int i = 0; i < countOfGas; i++) {
  140.             if (i != from) {
  141.                 citiesWeight[i] = Integer.MAX_VALUE;
  142.             }
  143.         }
  144.         while (!dijkstraQ.isEmpty()) {
  145.             int u = (int) dijkstraQ.poll();
  146.             citiesVisited[u] = true; //visited
  147.             // if(citiesWeight[u] == Integer.MAX) break; //nedostupny vrchol
  148.             //pro kazdyho souseda v vrcholu u
  149.             Set<Integer> keys = gas[u].keySet();
  150.             for (Integer v : keys) {//jestli soused v nebyl navstiven
  151.                 if (citiesVisited[v] == false) {//spocitej help = citiesWeight[u] + delka mezi uv
  152.                     int length = citiesWeight[u] + (int) gas[u].get(v);
  153.                     if (length < citiesWeight[v]) {   //nastav novou dvahu pro v a jeho predka
  154.                         dijkstraQ.remove(v);
  155.                         citiesWeight[v] = length;
  156.                         //prev[v] = u;
  157.                         dijkstraQ.add(v);
  158.                     }
  159.                 }
  160.             }
  161.         }
  162.         return citiesWeight[T];
  163.     }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement