jasonpogi1669

Transport (TLAC) Complete Solution using Java

Apr 21st, 2022
1,879
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.28 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.io.PrintWriter;
  5. import java.util.ArrayList;
  6. import java.util.Arrays;
  7. import java.util.Collections;
  8. import java.util.LinkedList;
  9. import java.util.StringTokenizer;
  10.  
  11. public class Solution {
  12.    
  13.     public static void main(String[] args) {   
  14.         FastScanner fs = new FastScanner();
  15.         PrintWriter out = new PrintWriter(System.out);
  16.         int n = fs.nextInt();
  17.         Graph g = new Graph(n);
  18.         g.readCities(fs);
  19.         g.addEdgeWeight(fs);
  20.         g.convertStartEnd(fs);
  21.         g.findPath();
  22.         g.printPath(out);
  23.         out.close();
  24.     }
  25.    
  26.     static class Graph {
  27.        
  28.         int n;
  29.         String[] cities;
  30.         int[] connected;
  31.         LinkedList<Integer> adj[];
  32.         int[][] cost;
  33.         boolean[] visited;
  34.         int start;
  35.         int end;
  36.         LinkedList<Integer> currentPath;
  37.         LinkedList<String> res;
  38.         int minWeight;
  39.         int minCount;
  40.        
  41.         Graph(int n) {
  42.             this.n = n;
  43.             cities = new String[n];
  44.             connected = new int[n];
  45.             adj = new LinkedList[n];
  46.             for(int i = 0; i < n; i++) {
  47.                 adj[i] = new LinkedList();
  48.             }
  49.             cost = new int[n][n];
  50.             for(int i = 0; i < n; i++) {
  51.                 Arrays.fill(cost[i], 0);
  52.             }
  53.             visited = new boolean[n];
  54.             Arrays.fill(visited, false);
  55.             currentPath = new LinkedList<>();
  56.             minWeight = Integer.MAX_VALUE;
  57.             minCount = Integer.MAX_VALUE;
  58.             res = new LinkedList();
  59.         }
  60.        
  61.         void readCities(FastScanner fs) {
  62.             for(int i = 0; i < n; i++) {
  63.                 cities[i] = fs.next();
  64.                 connected[i] = fs.nextInt();
  65.             }
  66.         }
  67.        
  68.         int convertCityToNum(String city) {
  69.             for(int i = 0; i < cities.length; i++) {
  70.                 if(city.equals(cities[i])) {
  71.                     return i;
  72.                 }
  73.             }
  74.             return -1;
  75.         }
  76.        
  77.         void addEdgeWeight(FastScanner fs) {
  78.             for(int u = 0; u < n; u++) {
  79.                 for(int i = 0; i < connected[u]; i++) {
  80.                     String city = fs.next();
  81.                     int weight = fs.nextInt();
  82.                     int v = convertCityToNum(city);
  83.                     adj[u].add(v);
  84.                     cost[u][v] = weight;
  85.                 }
  86.             }
  87.         }
  88.        
  89.         void convertStartEnd(FastScanner fs) {
  90.             start = convertCityToNum(fs.next());
  91.             end = convertCityToNum(fs.next());
  92.         }
  93.        
  94.         void findPath() {
  95.             DFS(start, end);
  96.         }
  97.        
  98.         void DFS(int u, int d) {
  99.             visited[u] = true;
  100.             currentPath.add(u);
  101.             if(u == d) {
  102.                 Object[] objectArray = currentPath.toArray();
  103.                 Integer[] path = Arrays.copyOf(objectArray, objectArray.length, Integer[].class);
  104.                 updatePath(path);
  105.             }
  106.             else {
  107.                 for(int v : adj[u]) {
  108.                     if(!visited[v]) {
  109.                         DFS(v, d);
  110.                     }
  111.                 }
  112.             }
  113.             visited[u] = false;
  114.             currentPath.removeLast();
  115.         }
  116.        
  117.         void updatePath(Integer[] path) {
  118.             int total = 0;
  119.             LinkedList<String> collect = new LinkedList<>();
  120.             int sz = path.length;
  121.             for(int i = 0; i < sz; i++) {
  122.                 if(i < sz - 1) {
  123.                     total += cost[path[i]][path[i+1]];
  124.                 }
  125.                 collect.add(cities[path[i]]);
  126.             }
  127.             if(total < minWeight) {
  128.                 minWeight = total;
  129.                 minCount = sz;
  130.                 res = collect;
  131.             }
  132.             else if(total == minWeight && sz < minCount) {
  133.                 minCount = sz;
  134.                 res = collect;
  135.             }
  136.         }
  137.        
  138.         void printPath(PrintWriter out) {
  139.             for(int i = 0; i < res.size(); i++) {
  140.                 if(i > 0) {
  141.                     out.print("->");
  142.                 }
  143.                 out.print(res.get(i));
  144.             }
  145.             out.println();
  146.         }
  147.        
  148.     }
  149.    
  150.     static void sort(int[] a) {
  151.         ArrayList<Integer> arr = new ArrayList<>();
  152.         for(int x : a) {
  153.             arr.add(x);
  154.         }
  155.         Collections.sort(arr);
  156.         for(int i = 0; i < a.length; i++) {
  157.             a[i] = arr.get(i);
  158.         }
  159.     }
  160.        
  161.     static class FastScanner {
  162.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  163.         StringTokenizer st = new StringTokenizer("");
  164.        
  165.         String next() {
  166.             while(!st.hasMoreTokens()) {
  167.                 try {
  168.                     st = new StringTokenizer(br.readLine());
  169.                 } catch(IOException e) {
  170.                     e.printStackTrace();
  171.                 }
  172.             }
  173.             return st.nextToken();
  174.         }
  175.        
  176.         int nextInt() {
  177.             return Integer.parseInt(next());
  178.         }
  179.        
  180.         int[] readArray(int n) {
  181.             int[] a = new int[n];
  182.             for(int i = 0; i < n; i++) {
  183.                 a[i] = nextInt();
  184.             }
  185.             return a;
  186.         }
  187.        
  188.         long nextLong() {
  189.             return Long.parseLong(next());
  190.         }
  191.        
  192.         double nextDouble() {
  193.             return Double.parseDouble(next());
  194.         }
  195.        
  196.         String nextLine() {
  197.             String str = "";
  198.             try {
  199.                 str = br.readLine();
  200.             } catch(IOException e) {
  201.                 e.printStackTrace();
  202.             }
  203.             return str;
  204.         }
  205.     }
  206. }
  207.  
Advertisement
Add Comment
Please, Sign In to add comment