hqt

Full Tank

hqt
Aug 19th, 2013
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.39 KB | None | 0 0
  1. package uva.shortestpath;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.BufferedWriter;
  5. import java.io.File;
  6. import java.io.InputStreamReader;
  7. import java.io.OutputStreamWriter;
  8. import java.io.PrintWriter;
  9. import java.util.ArrayList;
  10. import java.util.Comparator;
  11. import java.util.PriorityQueue;
  12. import java.util.Scanner;
  13.  
  14. public class FullTank {
  15.    
  16.     public static int INF = 100000000;
  17.     public static void main(String[] args) throws Exception {
  18.         FullTank main = new FullTank();
  19.         main.run();
  20.     }
  21.    
  22.     public void run() throws Exception {
  23.          Scanner sc = null;
  24.          PrintWriter pr = null;
  25.  
  26.          pr=new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
  27.          sc = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
  28.          sc = new Scanner(new File("input.txt"));
  29.          
  30.          int n = sc.nextInt();  // number of cities
  31.          int m = sc.nextInt();  // number of roads
  32.          
  33.          ArrayList<ArrayList<IntegerPairs>> adjList = new ArrayList<ArrayList<IntegerPairs>>();
  34.          for (int i = 0; i < n; i++) {
  35.              ArrayList<IntegerPairs> neighbor = new ArrayList<IntegerPairs>();
  36.              adjList.add(neighbor);
  37.          }
  38.          
  39.          int[] prices = new int[n]; // cost of fuel at each city
  40.          for (int i = 0; i < n; i++) prices[i] = sc.nextInt();
  41.  
  42.          // distance between city
  43.          for (int i = 0; i < m; i++) {
  44.              int u = sc.nextInt();
  45.              int v = sc.nextInt();
  46.              int c = sc.nextInt();
  47.              adjList.get(u).add(new IntegerPairs(v, c));
  48.              adjList.get(v).add(new IntegerPairs(u, c));
  49.          }
  50.          
  51.          // this array acts as optimized array in normal disjkstra
  52.          // put d outside for optimize
  53.          int[][] d = new int[1010][110];
  54.          int queries = sc.nextInt();
  55.          while(queries-- > 0) {
  56.              int capacity = sc.nextInt();
  57.              int start = sc.nextInt();
  58.              int end = sc.nextInt();
  59.              int res = -1;
  60.              
  61.              // Diskstra Implementation
  62.  
  63.              for (int i = 0; i < n; i++)  {
  64.                  for (int j = 0; j <= capacity; j++) {
  65.                      d[i][j] = INF;
  66.                  }
  67.              }
  68.              
  69.              PriorityQueue<State> pq = new PriorityQueue<State>(1, new Comparator<State>() {
  70.                 @Override
  71.                 public int compare(State s1, State s2) {
  72.                     return s1.val - s2.val;
  73.                 }
  74.             });
  75.              State initialState = new State(start, 0, 0);
  76.              pq.add(initialState);
  77.              d[start][0] = 0;
  78.              
  79.              while (!pq.isEmpty()) {
  80.                  State s = pq.poll();
  81.                  if (s.loc == end) {
  82.                     res = s.val;
  83.                     break; 
  84.                  }
  85.                  
  86.                  // if new state is not optimized as before. No need to use this state
  87.                  if (d[s.loc][s.fuel] < s.val) continue;
  88.                  
  89.                  // OPTIMIZE
  90.                  
  91.                  // buy one more fuel. add to queue.
  92.                  // (no need to buy until full capacity. just increase 1 step by step for optimize)
  93.                  if (s.fuel + 1 <= capacity && d[s.loc][s.fuel+1] > s.val + prices[s.loc]) {
  94.                      d[s.loc][s.fuel+1] = s.val + prices[s.loc];
  95.                      pq.add(new State(s.loc, s.fuel + 1, s.val + prices[s.loc]));
  96.                  }
  97.                  
  98.                  // check if can go to another vertex. If can. add into queue
  99.                  for(int i = 0; i < adjList.get(s.loc).size(); i++) {
  100.                      int u = adjList.get(s.loc).get(i).first();
  101.                      int distance = adjList.get(s.loc).get(i).second();
  102.                      if (s.fuel >= distance && d[u][s.fuel-distance] > s.val) {
  103.                          d[u][s.fuel-distance] = s.val;     // update new state
  104.                          pq.add(new State(u, s.fuel - distance, s.val));
  105.                      }
  106.                  }
  107.             }
  108.              
  109.              if (res != -1) {
  110.                  System.out.println(res);
  111.              }
  112.              else {
  113.                  System.out.println("impossible");
  114.              }
  115.              
  116.          }
  117.          
  118.          pr.close();
  119.          sc.close();
  120.     }
  121.    
  122.     public static class State {
  123.         int loc;
  124.         int fuel;
  125.         int val;
  126.         public State(int loc, int fuel, int val) { this.loc = loc; this.fuel = fuel; this.val =val;}
  127.     }
  128.    
  129.     public static class IntegerPairs implements Comparable<Object> {
  130.           Integer _first, _second;
  131.  
  132.           public IntegerPairs(Integer f, Integer s) {
  133.             _first = f;
  134.             _second = s;
  135.           }
  136.  
  137.           public int compareTo(Object o) {
  138.             if (this.first() != ((IntegerPairs )o).first())
  139.               return this.first() - ((IntegerPairs )o).first();
  140.             else
  141.               return this.second() - ((IntegerPairs )o).second();
  142.           }
  143.  
  144.           Integer first() { return _first; }
  145.           Integer second() { return _second; }
  146.         }
  147.  
  148.  
  149. }
Advertisement
Add Comment
Please, Sign In to add comment