[LeetCode] 815. Bus Routes 巴士路线

We have a list of bus routes. Each routes[i] is a bus route that the i-th bus repeats forever. For example if routes[0] = [1, 5, 7], this means that the first bus (0-th indexed) travels in the sequence 1->5->7->1->5->7->1->... forever.

We start at bus stop S (initially not on a bus), and we want to go to bus stop T. Travelling by buses only, what is the least number of buses we must take to reach our destination? Return -1 if it is not possible.

Example:
Input:
routes = [[1, 2, 7], [3, 6, 7]]
S = 1
T = 6
Output: 2
Explanation:
The best strategy is take the first bus to the bus stop 7, then take the second bus to the bus stop 6.

Note:

  • 1 <= routes.length <= 500.
  • 1 <= routes[i].length <= 500.
  • 0 <= routes[i][j] < 10 ^ 6.

有一个表示公交巴士路线的二维数组,每一个元素routes[i]代表每辆公交车循环运行的环形路线,求最少需要坐多少辆公交车才能从巴士站S到达T。

解法:BFS,先对原来的二维数组进行处理,二维数组的每一行代表这辆公交车能到达的站点,用HashMap记录某站点有哪个公交经过。这样处理完以后就知道每一个站点都有哪个公交车经过了。然后用一个queue记录当前站点,对于当前站点的所有经过的公交循环,每个公交又有自己的下一个站点。可以想象成一个图,每一个公交站点 被多少个公交经过,也就是意味着是个中转站,可以连通到另外一个公交上, 因为题目求的是经过公交的数量而不关心经过公交站点的数量,所以在BFS的时候以公交(也就是routes的下标)来扩展。

Java:

class Solution {
    public int numBusesToDestination(int[][] routes, int S, int T) {
       HashSet<Integer> visited = new HashSet<>();
       Queue<Integer> q = new LinkedList<>();
       HashMap<Integer, ArrayList<Integer>> map = new HashMap<>();
       int ret = 0; 

       if (S==T) return 0; 

       for(int i = 0; i < routes.length; i++){
            for(int j = 0; j < routes[i].length; j++){
                ArrayList<Integer> buses = map.getOrDefault(routes[i][j], new ArrayList<>());
                buses.add(i);
                map.put(routes[i][j], buses);
            }
        }

       q.offer(S);
       while (!q.isEmpty()) {
           int len = q.size();
           ret++;
           for (int i = 0; i < len; i++) {
               int cur = q.poll();
               ArrayList<Integer> buses = map.get(cur);
               for (int bus: buses) {
                    if (visited.contains(bus)) continue;
                    visited.add(bus);
                    for (int j = 0; j < routes[bus].length; j++) {
                        if (routes[bus][j] == T) return ret;
                        q.offer(routes[bus][j]);
                   }
               }
           }
        }
        return -1;
    }
}  

Java:

public int numBusesToDestination(int[][] routes, int S, int T) {
        HashMap<Integer, HashSet<Integer>> to_routes = new HashMap<>();
        for (int i = 0; i < routes.length; ++i)
            for (int j : routes[i]) {
                if (!to_routes.containsKey(j)) to_routes.put(j, new HashSet<Integer>());
                to_routes.get(j).add(i);
            }
        Queue<Point> bfs = new ArrayDeque();
        bfs.offer(new Point(S, 0));
        HashSet<Integer> seen = new HashSet<>();
        seen.add(S);
        while (!bfs.isEmpty()) {
            int stop = bfs.peek().x, bus = bfs.peek().y;
            bfs.poll();
            if (stop == T) return bus;
            for (int route_i : to_routes.get(stop))
                for (int next_stop : routes[route_i])
                    if (!seen.contains(next_stop)) {
                        seen.add(next_stop);
                        bfs.offer(new Point(next_stop, bus + 1));
                    }
        }
        return -1;
    }

Python:

def numBusesToDestination(self, routes, S, T):
        to_routes = collections.defaultdict(set)
        for i,route in enumerate(routes):
            for j in route: to_routes[j].add(i)
        bfs = [(S,0)]
        seen = set([S])
        for stop, bus in bfs:
            if stop == T: return bus
            for route_i in to_routes[stop]:
                for next_stop in routes[route_i]:
                    if next_stop not in seen:
                        bfs.append((next_stop, bus+1))
                        seen.add(next_stop)
                routes[route_i] = []
        return -1

Python:

# Time:  O(|V| + |E|)
# Space: O(|V| + |E|)

import collections

class Solution(object):
    def numBusesToDestination(self, routes, S, T):
        """
        :type routes: List[List[int]]
        :type S: int
        :type T: int
        :rtype: int
        """
        if S == T:
            return 0

        to_route = collections.defaultdict(set)
        for i, route in enumerate(routes):
            for stop in route:
                to_route[stop].add(i)

        result = 1
        q = [S]
        lookup = set([S])
        while q:
            next_q = []
            for stop in q:
                for i in to_route[stop]:
                    for next_stop in routes[i]:
                        if next_stop in lookup:
                            continue
                        if next_stop == T:
                            return result
                        next_q.append(next_stop)
                        to_route[next_stop].remove(i)
                        lookup.add(next_stop)
            q = next_q
            result += 1

        return -1  

C++:

int numBusesToDestination(vector<vector<int>>& routes, int S, int T) {
        unordered_map<int, unordered_set<int>> to_route;
        for (int i = 0; i < routes.size(); ++i) for (auto& j : routes[i]) to_route[j].insert(i);
        queue<pair<int, int>> bfs; bfs.push(make_pair(S, 0));
        unordered_set<int> seen = {S};
        while (!bfs.empty()) {
            int stop = bfs.front().first, bus = bfs.front().second;
            bfs.pop();
            if (stop == T) return bus;
            for (auto& route_i : to_route[stop]) {
                for (auto& next_stop : routes[route_i])
                    if (seen.find(next_stop) == seen.end()) {
                        seen.insert(next_stop);
                        bfs.push(make_pair(next_stop, bus + 1));
                    }
                routes[route_i].clear();
            }
        }
        return -1;
    }

  

原文地址:https://www.cnblogs.com/lightwindy/p/9802088.html

时间: 2024-08-08 07:28:18

[LeetCode] 815. Bus Routes 巴士路线的相关文章

Leetcode 815. Bus Routes

Problem: We have a list of bus routes. Each routes[i] is a bus route that the i-th bus repeats forever. For example if routes[0] = [1, 5, 7], this means that the first bus (0-th indexed) travels in the sequence 1->5->7->1->5->7->1->..

815. Bus Routes

问题描述: We have a list of bus routes. Each routes[i] is a bus route that the i-th bus repeats forever. For example if routes[0] = [1, 5, 7], this means that the first bus (0-th indexed) travels in the sequence 1->5->7->1->5->7->1->... f

[LeetCode] Bus Routes 公交线路

We have a list of bus routes. Each routes[i] is a bus route that the i-th bus repeats forever. For example if routes[0] = [1, 5, 7], this means that the first bus (0-th indexed) travels in the sequence 1->5->7->1->5->7->1->... forever

hdu 5552 Bus Routes

hdu 5552 Bus Routes 考虑有环的图不方便,可以考虑无环连通图的数量,然后用连通图的数量减去就好了. 无环连通图的个数就是树的个数,又 prufer 序我们知道是 $ n^{n-2} $ 其中又由于有 $ n-1 $ 个边,每个边可以涂色,所以总共无环的方案数量是 $ m^{n-1} n^{n-2} $ 那么现在就要算连通图的数量了.这个不如不连通图的数量好算. 不连通图的数量怎么算呢,原本想的是容斥,但是貌似不好实现,看了题解发现一种神仙思路.考虑固定一个点,并且让这个点连出一

LeetCode:Minumus Path Sum(矩阵路线的元素最小值)

Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time. 思路:与求两个字符串的最长公共子序列的长度思路一致. 状态转移方程

算法与数据结构基础 - 广度优先搜索(BFS)

BFS基础 广度优先搜索(Breadth First Search)用于按离始节点距离.由近到远渐次访问图的节点,可视化BFS 通常使用队列(queue)结构模拟BFS过程,关于queue见:算法与数据结构基础 - 队列(Queue) 最直观的BFS应用是图和树的遍历,其中图常用邻接表或矩阵表示,例如 LeetCode题目 690. Employee Importance: // LeetCode 690. Employee Importance/* class Employee { publi

google onsite 1

Given a 2D matrix , with obstacles. Start from any of the first row, exit from any of the last row. What is the minimum steps? 000001 000100 000000 000100 step 2. Given a list of buslines, and the stops of each busline, given a start stop, and a end

【LeetCode】BFS(共43题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [101]Symmetric Tree 判断一棵树是不是对称. 题解:直接递归判断了,感觉和bfs没有什么强联系,当然如果你一定要用queue改写的话,勉强也能算bfs. // 这个题目的重点是 比较对象是 左子树的左儿子和右子树的右儿子, 左子树的右儿子和右子树的左儿子.不要搞错. // 直接中序遍历的话会有错的情况,最蠢的情况是数字标注改一改.. 1 /** 2

URAL 1137Bus Routes (dfs)

Z - Bus Routes Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice URAL 1137 Description Several bus routes were in the city of Fishburg. None of the routes shared the same section of road, though commo