Advertisement
Guest User

Untitled

a guest
Jun 4th, 2017
1,450
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.96 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3. import java.text.*;
  4. import java.math.*;
  5. import java.util.regex.*;
  6.  
  7.  
  8. public class Solution {
  9.  
  10.     public static class Graph {
  11.         //key = node. value = list of neighbors.
  12.         Map<Integer, List<Integer>> nodes;
  13.  
  14.         public Graph(int size) {
  15.             nodes = new HashMap<Integer, List<Integer>>();
  16.             for(int i = 0; i<size; i++){
  17.                 nodes.put(i, new ArrayList<Integer>());
  18.             }
  19.         }
  20.  
  21.         public void addEdge(int first, int second) {
  22.             if(first != second){
  23.                 if(!(nodes.get(first).contains(second))){
  24.                     nodes.get(first).add(second);
  25.                 }
  26.                 if(!(nodes.get(second).contains(first))){
  27.                     nodes.get(second).add(first);
  28.                 }
  29.             }
  30.         }
  31.  
  32.         public int[] shortestReach(int startId) { // 0 indexed
  33.             int[] distances = new int[nodes.keySet().size()];
  34.             Arrays.fill(distances, -1);
  35.             distances[startId] = 0;
  36.             visitNeighbors(startId, distances);
  37.             return distances;
  38.         }
  39.  
  40.         private void visitNeighbors(int startId, int[] distances){
  41.             List<Integer> nodesToVisit = new ArrayList<Integer>();
  42.  
  43.             nodesToVisit.add(startId);
  44.             distances[startId] = 0;
  45.             while (!nodesToVisit.isEmpty()) {
  46.                 int current = nodesToVisit.get(0);
  47.                 nodesToVisit.remove(0);
  48.                 for (int i : nodes.get(current)) {
  49.                     if (distances[i] == -1) {
  50.                         distances[i] = distances[current] + 6;
  51.                         nodesToVisit.add(i);
  52.                     }
  53.                     //dont recurse right here, otherwise it will become depth-first and we will not get shortest path.
  54.                 }
  55.             }
  56.         }
  57.     }
  58.  
  59.     public static void main(String[] args) {
  60.         Scanner scanner = new Scanner(System.in);
  61.  
  62.         int queries = scanner.nextInt();
  63.  
  64.         for (int t = 0; t < queries; t++) {
  65.  
  66.             // Create a graph of size n where each edge weight is 6:
  67.             Graph graph = new Graph(scanner.nextInt());
  68.             int m = scanner.nextInt();
  69.  
  70.             // read and set edges
  71.             for (int i = 0; i < m; i++) {
  72.                 int u = scanner.nextInt() - 1;
  73.                 int v = scanner.nextInt() - 1;
  74.  
  75.                 // add each edge to the graph
  76.                 graph.addEdge(u, v);
  77.             }
  78.  
  79.             // Find shortest reach from node s
  80.             int startId = scanner.nextInt() - 1;
  81.             int[] distances = graph.shortestReach(startId);
  82.  
  83.             for (int i = 0; i < distances.length; i++) {
  84.                 if (i != startId) {
  85.                     System.out.print(distances[i]);
  86.                     System.out.print(" ");
  87.                 }
  88.             }
  89.             System.out.println();
  90.         }
  91.  
  92.         scanner.close();
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement