leetcode 815

  1. 公交路线
    给你一个数组 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 。
示例 2:

输入:routes = [[7,12],[4,5,15],[6],[15,19],[9,12,13]], source = 15, target = 12
输出:-1

方法1:以站点为节点

class Solution {
public:
    int numBusesToDestination(vector<vector<int>>& routes, int source, int target) {
        int len = routes.size();
        if(source == target) return 0;
        map<int , vector<int>> dic;//dic[i][j]标表示经过站点i的某条线路
        vector<bool> arr(len , false);//arr[i]表示线路i是否被遍历过
        for(int i = 0 ; i < len ; i++)
        {
            for(auto site : routes[i]) dic[site].push_back(i);
        }
        queue<int> q;//需要遍历的站点队列
        q.push(source);
        int step = 0;
        while(!q.empty())
        {
            step++;
            int n = q.size();
            for(int k = 0 ; k < n ; k++)
            {
                int site = q.front();
                q.pop();
                for(int i = 0 ; i < dic[site].size() ; i++)
                {
                    int line = dic[site][i];
                    if(!arr[line])//线路未被遍历过
                        for(int j = 0 ; j < routes[line].size() ; j++)
                        {
                            if(routes[line][j] == target)
                            {
                                return step;
                            }
                            if(routes[line][j] != site)
                                q.push(routes[line][j]);
                        }
                        arr[line] = true;
                }
            }
        }
        return -1;
    }
};

方法2:以线路为节点,以线路为节点bfs时不需遍历节点,时间复杂度优于方法1。

class Solution {
public:
    int numBusesToDestination(vector<vector<int>>& routes, int source, int target) {
        if(source == target) return 0;
        int len = routes.size();
        int edge[len][len];//edge[i][] 表示含点i的边
        memset(edge , 0 , sizeof(edge));
        map<int , vector<int>> dic;//dic[i][]表示所有经过站点i的线路
        for(int i = 0 ; i < len ; i++)
        {
            for(int site : routes[i])
            {
                for(int j : dic[site])
                {
                    edge[i][j] = edge[j][i] = 1;//把相连的线路连接成图
                }
                dic[site].push_back(i);//把经过站点i的线路加进dic中
            }
        }
        int dis[len];//dic[i]表示从source到线路i的最短距离,即需要线路数
        memset(dis , -1 , sizeof(dis));//初始化-1,表示source不能到达线路i
        queue<int> q;
        vector<bool> arr(len , false);//记录线路i是否被走过
        for(int line : dic[source])
        {
            dis[line] = 1;//包含source站点的线路 ,从source到该线路的距离为1
            q.push(line);
        }
        while(!q.empty())
        {
            int n = q.size();
            for(int i = 0 ; i < n ; i++)
            {
                int a = q.front();
                q.pop();
                if(arr[a]) continue;
                for(int b = 0 ; b < len ; b++)
                {
                    if(b == a) continue;
                    if(edge[a][b] && dis[b] == -1)//a b成边,且source到b的距离未计算过
                    {
                        dis[b] = dis[a] + 1;
                        q.push(b);
                    }
                }
                arr[a] = true;
            }
        }
        int ans = INT_MAX;
        for(int line : dic[target])
        {
            if(dis[line] != -1) ans = min(ans , dis[line]);
        }
        return ans == INT_MAX ? -1 : ans;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值