yulya3102

4A

Oct 23rd, 2012
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.72 KB | None | 0 0
  1. import com.sun.jmx.remote.internal.ArrayQueue;
  2.  
  3. import java.io.*;
  4. import java.util.ArrayList;
  5. import java.util.StringTokenizer;
  6.  
  7. /**
  8.  * Created with IntelliJ IDEA.
  9.  * User: yulya3102
  10.  * Date: 23.10.12
  11.  * Time: 16:54
  12.  * To change this template use File | Settings | File Templates.
  13.  */
  14. public class A {
  15.     public static void main(String[] args) throws IOException {
  16.         BufferedReader in = new BufferedReader(new FileReader("pathbge1.in"));
  17.         StringTokenizer str = new StringTokenizer(in.readLine());
  18.         int n = Integer.parseInt(str.nextToken());
  19.         int m = Integer.parseInt(str.nextToken());
  20.         Graph graph = new Graph(n);
  21.         for (int i = 0; i < m; i++) {
  22.             str = new StringTokenizer(in.readLine());
  23.             int from = Integer.parseInt(str.nextToken()) - 1;
  24.             int to = Integer.parseInt(str.nextToken()) - 1;
  25.             graph.add(from, to);
  26.         }
  27.         int[] d = graph.bfs(0);
  28.         PrintWriter out = new PrintWriter(new File("pathbge1.out"));
  29.         for (int i = 0; i < n; i++)
  30.             out.print(d[i] + " ");
  31.         out.close();
  32.     }
  33.  
  34.     private static class Graph {
  35.         class AdjacencyList {
  36.             private ArrayList<Integer> list;
  37.  
  38.             public AdjacencyList() {
  39.                 list = new ArrayList<Integer>();
  40.             }
  41.  
  42.             public int size() {
  43.                 return list.size();
  44.             }
  45.  
  46.             public void add(int to) {
  47.                 list.add(to);
  48.             }
  49.  
  50.             public int get(int index) {
  51.                 return list.get(index);
  52.             }
  53.         }
  54.  
  55.         AdjacencyList[] adjacencyLists;
  56.         int n;
  57.         int d[];
  58.         ArrayQueue<Integer> queue;
  59.  
  60.         public Graph(int n) {
  61.             this.n = n;
  62.             d = new int[n];
  63.             adjacencyLists = new AdjacencyList[n];
  64.             for (int i = 0; i < n; i++) {
  65.                 d[i] = Integer.MAX_VALUE;
  66.                 adjacencyLists[i] = new AdjacencyList();
  67.             }
  68.             queue = new ArrayQueue<Integer>(n);
  69.         }
  70.  
  71.         public void add(int from, int to) {
  72.             adjacencyLists[from].add(to);
  73.             adjacencyLists[to].add(from);
  74.         }
  75.  
  76.         public int[] bfs(int s) {
  77.             d[s] = 0;
  78.             queue.add(s);
  79.             while (!queue.isEmpty()) {
  80.                 int u = queue.remove(0);
  81.                 for (int i = 0; i < adjacencyLists[u].size(); i++) {
  82.                     int v = adjacencyLists[u].get(i);
  83.                     if (d[v] == Integer.MAX_VALUE) {
  84.                         d[v] = d[u] + 1;
  85.                         queue.add(v);
  86.                     }
  87.                 }
  88.             }
  89.             return d;
  90.         }
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment