Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- class Vertex {
- private int number;
- private int distance;
- private Vertex previous;
- private HashMap<Integer, Integer> edges;
- public Vertex(int vertex) {
- this.number = vertex;
- this.distance = Integer.MAX_VALUE;
- this.previous = null;
- this.edges = new HashMap<>();
- }
- public int getNumber() {
- return number;
- }
- public int getDistance() {
- return distance;
- }
- public void setDistance(int distance) {
- this.distance = distance;
- }
- public Vertex getPrevious() {
- return previous;
- }
- public void setPrevious(Vertex previous) {
- this.previous = previous;
- }
- public HashMap<Integer, Integer> getEdges() {
- return edges;
- }
- public void setEdges(HashMap<Integer, Integer> edges) {
- this.edges = edges;
- }
- public String toString() {
- return Integer.toString(number) + "/" + Integer.toString(distance);
- }
- }
- class VertexComparator implements Comparator<Vertex> {
- @Override
- public int compare(Vertex o1, Vertex o2) {
- return o1.getDistance() - o2.getDistance();
- }
- }
- class Solution{
- public static int n;
- public static int m;
- public static int begin;
- public static int end;
- public static HashMap<Integer, Vertex> graph;
- public static PriorityQueue<Vertex> notVisited;
- // Implement the solve method to return the answer to the problem posed by the inputstream.
- public String solve(InputStream in) {
- BufferedReader br = null;
- br = new BufferedReader(new InputStreamReader(in));
- Comparator<Vertex> compare = new VertexComparator();
- notVisited = new PriorityQueue<>(1, compare);
- String[] s = new String[0];
- try {
- s = br.readLine().split(" ");
- } catch (IOException e) {
- e.printStackTrace();
- }
- n = Integer.parseInt(s[0]);
- graph = new HashMap<>(n);
- m = Integer.parseInt(s[1]);
- begin = Integer.parseInt(s[2]);
- for(int i = 1; i <= n; i++){
- Vertex v = new Vertex(i);
- graph.put(i, v);
- if(i == begin){
- v.setDistance(0);
- }
- notVisited.add(v);
- }
- end = Integer.parseInt(s[3]);;
- if(begin == end || m == 0) {
- try {
- br.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return "0";
- }
- String read = null;
- try {
- while((read = br.readLine()) != null) {
- String[] s2 = read.split(" ");
- int v = Integer.parseInt(s2[0]);
- graph.get(v).getEdges().put(Integer.parseInt(s2[1]), Integer.parseInt(s2[2]));
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- // System.out.println(notVisited.toString());
- // System.out.println(graph.toString());
- return new Solution().recur();
- }
- public String recur() {
- if(notVisited.isEmpty()){
- return "-1";
- }
- Vertex min = null;
- while(!notVisited.isEmpty()) {
- min = notVisited.poll();
- if(min.getNumber() == end)
- break;
- if (!min.getEdges().isEmpty()) {
- for (Map.Entry<Integer, Integer> child : min.getEdges().entrySet()) {
- Vertex edge = graph.get(child.getKey());
- int diff = min.getDistance() + child.getValue() + min.getEdges().size();
- if (diff < edge.getDistance()) {
- notVisited.remove(edge);
- edge.setDistance(diff);
- edge.setPrevious(min);
- notVisited.add(edge);
- }
- }
- }
- }
- return Integer.toString(min.getDistance());
- }
- // You can leave the following method unchanged.
- public static String run(InputStream in) {
- return new Solution().solve(in);
- }
- }
- //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement