zoukankan      html  css  js  c++  java
  • 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;
        }
    };
  • 相关阅读:
    结巴分词 0.14 版发布,Python 中文分词库
    Lazarus 1.0.2 发布,Pascal 集成开发环境
    Android全屏 去除标题栏和状态栏
    服务器日志现 Android 4.2 传将添多项新特性
    Percona XtraBackup 2.0.3 发布
    长平狐 Android 强制设置横屏或竖屏 设置全屏
    NetBeans 7.3 Beta 发布,全新的 HTML5 支持
    CppDepend现在已经支持Linux
    GromJS 1.7.18 发布,服务器端的 JavaScript
    Apache OpenWebBeans 1.1.6 发布
  • 原文地址:https://www.cnblogs.com/stepping/p/14948849.html
Copyright © 2011-2022 走看看