• 815. 公交路线 力扣(困难) BFS,节点


    题目描述:

    给你一个数组 routes ,表示一系列公交线路,其中每个 routes[i] 表示一条公交线路,第 i 辆公交车将会在上面循环行驶。

    例如,路线 routes[0] = [1, 5, 7] 表示第 0 辆公交车会一直按序列 1 -> 5 -> 7 -> 1 -> 5 -> 7 -> 1 -> ... 这样的车站路线行驶。
    现在从 source 车站出发(初始时不在公交车上),要前往 target 车站。 期间仅可乘坐公交车。

    求出 最少乘坐的公交车数量 。如果不可能到达终点车站,返回 -1 。

    示例 1:

    输入:routes = [[1,2,7],[3,6,7]], source = 1, target = 6
    输出:2
    解释:最优策略是先乘坐第一辆公交车到达车站 7 , 然后换乘第二辆公交车到车站 6 。

    题源:https://leetcode-cn.com/problems/bus-routes/

    代码:

    class Solution {
    public:
       
        int numBusesToDestination(vector<vector<int>>& routes, int source, int target) {
            if(source==target) return 0;
    
            //int team[1000005];
            int dis[505];
            set<int> mp[505];
            int num=0,res=0x7fffffff;
            vector<int>  station[1000005];
            
            memset(dis,-1,sizeof(dis));
        
            for(int i=0;i<routes.size();i++)
              for(int j=0;j<routes[i].size();j++)
                  station[routes[i][j]].push_back(i);  // 统计每个站,分别有哪几路车经过。为后面建图准备
       
            for(int i=0;i<routes.size();i++)
              for(int j=0;j<routes[i].size();j++)
              {
                  int nowstation=routes[i][j];
                  num=max(num,routes[i][j]);
                  for(int t=0;t<station[nowstation].size();t++)
                  {
                      mp[i].insert(station[nowstation][t]);
                      mp[station[nowstation][t]].insert(i);
                  }
              }
    // 把起始点所在的路线扔进queue queue
    <int> Q; for(int i=0;i<station[source].size();i++) { Q.push(station[source][i]); dis[station[source][i]]=1; }     
    // bfs计算从起始点所在路线到其他路线的最短距离
    while(!Q.empty()) { int p=Q.front(); Q.pop(); for(set<int>::iterator i=mp[p].begin();i!=mp[p].end();i++) { if(dis[*i]>0) continue; Q.push(*i); dis[*i]=dis[p]+1; } }

         // 遍历终点所在的所有路线,找出距离最近的那条
    for(int i=0;i<station[target].size();i++) res=min(res,dis[station[target][i]]); if (res==0x7fffffff) return -1; else return res; } };

     耗时,不知道哪里优化一下

    题解:https://www.icode9.com/content-4-625444.html

    // 执行用时 :116 ms, 在所有 C++ 提交中击败了96.52%的用户
    // 内存消耗 :34.4 MB, 在所有 C++ 提交中击败了67.84%的用户
    class Solution {
    public:
        int numBusesToDestination(vector<vector<int>>& routes, int S, int T) {
            if (S == T) return 0;
            int res = 0;
            unordered_map<int, vector<int>> stop2bus;
            queue<int> q{{S}};
            unordered_set<int> visited;
            for (int i = 0; i < routes.size(); ++i) {
                for (int j : routes[i]) {
                    stop2bus[j].push_back(i);
                }
            }
            while (!q.empty()) {
                ++res;
                for (int i = q.size(); i > 0; --i) {
                    int t = q.front(); q.pop();
                    for (int bus : stop2bus[t]) {
                        if (visited.count(bus)) continue;
                        visited.insert(bus);
                        for (int stop : routes[bus]) {
                            if (stop == T) return res;
                            q.push(stop);
                        }
                    }
                }
            }
            return -1;
        }
    };
  • 相关阅读:
    [Give a try | Multithreading]The Practical Guide to Multithreading
    [SOLID]The Principles of OOD
    Mixing Native and Managed Types in C++
    [转载]关于大型软件重构的一些想法
    Mixed mode programming is the absolute power of C++/CLI
    Sample: Mixing Unmanaged C++, C++/CLI, and C# code
    添加删除虚函数带来的问题及解决办法
    如何阅读code base的组内讨论的总结
    如何快速定位一个函数的返回点(c/c++ Only)
    DirextX Training笔记
  • 原文地址:https://www.cnblogs.com/stepping/p/14948849.html
Copyright © 2020-2023  润新知