How does a Breadth-First Search work when looking for Shortest Path?

前端 未结 8 1469
太阳男子
太阳男子 2020-11-30 16:52

I\'ve done some research, and I seem to be missing one small part of this algorithm. I understand how a Breadth-First Search works, but I don\'t understand how exactly it wi

8条回答
  •  借酒劲吻你
    2020-11-30 17:10

    The following solution works for all the test cases.

    import java.io.*;
    import java.util.*;
    import java.text.*;
    import java.math.*;
    import java.util.regex.*;
    
    public class Solution {
    
       public static void main(String[] args)
            {
                Scanner sc = new Scanner(System.in);
    
                int testCases = sc.nextInt();
    
                for (int i = 0; i < testCases; i++)
                {
                    int totalNodes = sc.nextInt();
                    int totalEdges = sc.nextInt();
    
                    Map> adjacencyList = new HashMap>();
    
                    for (int j = 0; j < totalEdges; j++)
                    {
                        int src = sc.nextInt();
                        int dest = sc.nextInt();
    
                        if (adjacencyList.get(src) == null)
                        {
                            List neighbours = new ArrayList();
                            neighbours.add(dest);
                            adjacencyList.put(src, neighbours);
                        } else
                        {
                            List neighbours = adjacencyList.get(src);
                            neighbours.add(dest);
                            adjacencyList.put(src, neighbours);
                        }
    
    
                        if (adjacencyList.get(dest) == null)
                        {
                            List neighbours = new ArrayList();
                            neighbours.add(src);
                            adjacencyList.put(dest, neighbours);
                        } else
                        {
                            List neighbours = adjacencyList.get(dest);
                            neighbours.add(src);
                            adjacencyList.put(dest, neighbours);
                        }
                    }
    
                    int start = sc.nextInt();
    
                    Queue queue = new LinkedList<>();
    
                    queue.add(start);
    
                    int[] costs = new int[totalNodes + 1];
    
                    Arrays.fill(costs, 0);
    
                    costs[start] = 0;
    
                    Map visited = new HashMap();
    
                    while (!queue.isEmpty())
                    {
                        int node = queue.remove();
    
                        if(visited.get(node +"") != null)
                        {
                            continue;
                        }
    
                        visited.put(node + "", 1);
    
                        int nodeCost = costs[node];
    
                        List children = adjacencyList.get(node);
    
                        if (children != null)
                        {
                            for (Integer child : children)
                            {
                                int total = nodeCost + 6;
                                String key = child + "";
    
                                if (visited.get(key) == null)
                                {
                                    queue.add(child);
    
                                    if (costs[child] == 0)
                                    {
                                        costs[child] = total;
                                    } else if (costs[child] > total)
                                    {
                                        costs[child] = total;
                                    }
                                }
                            }
                        }
                    }
    
                    for (int k = 1; k <= totalNodes; k++)
                    {
                        if (k == start)
                        {
                            continue;
                        }
    
                        System.out.print(costs[k] == 0 ? -1 : costs[k]);
                        System.out.print(" ");
                    }
                    System.out.println();
                }
            }
    }
    

提交回复
热议问题