@@ -0,0 +1,2640 @@
+//https://www.hackerrank.com/challenges/bfsshortreach%0Aimport java.io.*;%0Aimport java.util.*;%0A%0Apublic class Solution %7B%0A  public static void main(String%5B%5D args) throws IOException %7B%0A    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));%0A    %0A    //For each test case%0A    for(byte T = Byte.parseByte(br.readLine()); T %3E 0; --T)%7B%0A      %0A      //Get number of nodes (N) and number of edges (M)%0A      String%5B%5D line = br.readLine().split(%22 %22);%0A      final short N = Short.parseShort(line%5B0%5D);%0A      final int M = Integer.parseInt(line%5B1%5D);%0A      %0A      //Create nodes%0A      final Node%5B%5D nodes = new Node%5BN%5D;%0A      for(short i = 0; i %3C N; ++i)%7B%0A        nodes%5Bi%5D = new Node(i);%0A      %7D%0A      %0A      //Get edges%0A      for(int i = 0; i %3C M; ++i)%7B%0A        line = br.readLine().split(%22 %22);%0A        final short x = (short)(Short.parseShort(line%5B0%5D) - 1);%0A        final short y = (short)(Short.parseShort(line%5B1%5D) - 1);%0A        nodes%5Bx%5D.edges.add(new Edge((short)6, nodes%5By%5D));%0A        nodes%5By%5D.edges.add(new Edge((short)6, nodes%5Bx%5D));%0A      %7D%0A      %0A      //Get starting node id%0A      final short S = (short)(Short.parseShort(br.readLine().trim()) - 1);%0A      %0A      //Solve%0A      final short%5B%5D minDistances = getMinDistances(N, S, nodes);%0A      %0A      //Print output%0A      StringBuffer sb = new StringBuffer();%0A      for(int i = 0; i %3C N; ++i)%7B%0A        if(i != S)%7B%0A          sb.append(minDistances%5Bi%5D + %22 %22);%0A        %7D%0A      %7D%0A      System.out.println(sb);%0A    %7D%0A  %7D%0A  %0A  private static short%5B%5D getMinDistances(final short N, final short S, final Node%5B%5D nodes)%7B%0A    %0A    //Initialize min distances%0A    final short%5B%5D minDistances = new short%5BN%5D;%0A    for(short i = 0; i %3C N; minDistances%5Bi++%5D = -1)%7B%7D%0A    minDistances%5BS%5D = 0;%0A    %0A    //Follow edges and record min distances to starting node%0A    final Queue%3CNode%3E q = new LinkedList%3CNode%3E();%0A    q.add(nodes%5BS%5D);%0A    do%7B%0A      final Node node = q.poll();%0A      final short curDistance = minDistances%5Bnode.id%5D;%0A      for(Edge edge : node.edges)%7B%0A        if(minDistances%5Bedge.node.id%5D %3C 0)%7B%0A          q.add(edge.node);%0A          minDistances%5Bedge.node.id%5D = (short)(edge.weight + curDistance);%0A        %7D%0A      %7D%0A    %7D while (!q.isEmpty());%0A    %0A    return minDistances;%0A  %7D%0A  %0A  private static class Node %7B%0A    public final short id;%0A    public final List%3CEdge%3E edges;%0A    public Node(final short id)%7B%0A      this.id = id;%0A      this.edges = new LinkedList%3CEdge%3E();%0A    %7D%0A  %7D%0A  %0A  private static class Edge %7B%0A    public final short weight;%0A    public final Node node;%0A    public Edge(final short weight, final Node node)%7B%0A      this.weight = weight;%0A      this.node = node;%0A    %7D%0A  %7D%0A%7D%0A

Create bfsshortreach.java