Advertisement
apatel256

Untitled

Nov 12th, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.98 KB | None | 0 0
  1. In this problem, we will be writing our own graph search algorithm.
  2.  
  3. It is the year 3000 and the greedy government has added tolls to all the roads! You are trying to get to GSU to take Professor Kumar's exam, but don't want to spend a lot of money on these silly tolls. You are leaving quite early in the morning, so time is not a concern.
  4.  
  5. There are up to 8 possible tolls, but you don't need to visit all 8. You just need to get to toll H as it is the closest one to GSU. You will always start at toll A.
  6.  
  7. When reading the input, the first line will be the number of edges. Each edge would come on its own line, in the format of Start End Toll.
  8.  
  9. Expected Output is the minimum number of dollars required for the trip.
  10.  
  11. I have written the input/output writing code for you. You can rewrite it if you'd like, but you do not need to.
  12.  
  13. Your findCheapestPath function is given a List> as input. This could look like
  14.  
  15. List[0] = ["A", "B", "3"];
  16. List[1] = ["A", "C", "4"];
  17. List[2] = ["C", "H", "2"];
  18. List[3] = ["B", "H", "9"];
  19. A good step one would be to use this input list to create some nodes and edges, and then write some kind of search to find your end node! Good luck!
  20.  
  21. Input Format
  22.  
  23. 4
  24. A B 3
  25. A C 4
  26. C H 2
  27. B H 9
  28. Constraints
  29.  
  30. You may use use imports from java.util.*. You can assume there are no infinite loops, and that, the graph is finite (At most 8 tolls). There will also always be a path from A to H. If you are unable to write the code to find the smallest path, consider just finding a path that has weight less than 10. This will give you a large majority of the points (most of the test cases!). Hint: All of the repl.its have been left up!
  31.  
  32. Output Format
  33.  
  34. 6
  35.  
  36. Sample Input 0
  37.  
  38. 4
  39. A B 3
  40. A C 4
  41. C H 2
  42. B H 9
  43. Sample Output 0
  44.  
  45. 6
  46. Explanation 0
  47.  
  48. The graph is of A, B, C, H. A points to B and C, with weights 3 and 4 respectively. C and B point to H, with weights 2 and 9 respectively. Shortest path from A-H would be A-C (4) + C-H(2) = 6.
  49.  
  50.  
  51. implement code into this:
  52.  
  53. import java.io.*;
  54. import java.math.*;
  55. import java.security.*;
  56. import java.text.*;
  57. import java.util.*;
  58. import java.util.concurrent.*;
  59. import java.util.regex.*;
  60.  
  61. class Result {
  62.  
  63.  
  64. /*
  65. * Complete the 'findCheapestPath' function below.
  66. *
  67. * The function is expected to return an INTEGER.
  68. * The function accepts 2D_STRING_ARRAY edges as parameter.
  69. */
  70.  
  71. public static int findCheapestPath(List<List<String>> edges) {
  72. // Write your code here
  73. HashMap<String, Node> map = new HashMap<>();
  74. for (List<String> edge: edges){
  75. Node a = map.get(edge.get(0)); // Start node;
  76. if (a == null){
  77. a = new Node(edge.get(0));
  78. map.put(edge.get(0),a);
  79. }
  80. Node b = map.get(edge.get(1)): //End Node;
  81. if (b == null){
  82. b = new Node(edge.get(1));
  83. map.put(edge.get(1), b);
  84. }
  85. //Add edge
  86. a.add(NodeEdge(b, Integer.parseInt(edge.get(2))));
  87. }
  88. // TODO: Find path from A to H:
  89.  
  90. return dfs(map.get("A"), map.get("H"));
  91.  
  92. }
  93. public static int dfs(Node a, Node b, int weightSoFar){
  94. //Return true only if you get a==b and weightSoFar<=10
  95. }
  96. }
  97.  
  98. class Node {
  99. String value;
  100. ArrayList<NodeEdge> edges;
  101. public Node(String value){
  102. edges = new ArrayList<>();
  103. this.value = value;
  104. }
  105. public void add(NodeEdge edge){
  106. edge.add(edge);
  107. }
  108. }
  109.  
  110. class NodeEdge{
  111. Node end;
  112. int weight;
  113. public NodeEdge(Node end, int weight){
  114. this.end = end;
  115. this.weight = weight;
  116. }
  117. }
  118.  
  119. public class Solution {
  120. public static void main(String[] args) throws IOException {
  121. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
  122. BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
  123.  
  124. int n = Integer.parseInt(bufferedReader.readLine().trim());
  125.  
  126. List<List<String>> arr = new ArrayList<>();
  127.  
  128. for (int i = 0; i < n; i++) {
  129. arr.add(Arrays.asList(bufferedReader.readLine().replaceAll("\\s+$", "").split(" ")));
  130. }
  131.  
  132. int result = Result.findCheapestPath(arr);
  133.  
  134. bufferedWriter.write(String.valueOf(result));
  135. bufferedWriter.newLine();
  136.  
  137. bufferedReader.close();
  138. bufferedWriter.close();
  139. }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement