其他分享
首页 > 其他分享> > 815. Bus Routes

815. Bus Routes

作者:互联网

问题:

给定多条bus线路,routes[i]代表 bus i 的所有经停站点。

给定source起始站,和target目标站,求最少换乘几辆车能到达目的地。

Example 1:
Input: routes = [[1,2,7],[3,6,7]], source = 1, target = 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.

Example 2:
Input: routes = [[7,12],[4,5,15],[6],[15,19],[9,12,13]], source = 15, target = 12
Output: -1
 
Constraints:
1 <= routes.length <= 500.
1 <= routes[i].length <= 105
All the values of routes[i] are unique.
sum(routes[i].length) <= 105
0 <= routes[i][j] < 106
0 <= source, target < 106

  

解法:BFS

分析:本问题中,

因此可使用两个visited数组,来标记已经乘坐过的线路or经过的站点。

 

通过给定的线路信息,构造站点上的所有bus结构体

 

将当前所在的站点,入队。

遍历队列,

 

代码参考:

 1 class Solution {
 2 public:
 3     int numBusesToDestination(vector<vector<int>>& routes, int source, int target) {
 4         unordered_map<int,vector<int>> stop_buses;//dest, bus_no
 5         for(int i=0; i<routes.size(); i++) {//bus[i]
 6             for(auto stp:routes[i]) {//every stop in this bus
 7                 stop_buses[stp].push_back(i);
 8             }
 9         }
10         int res = 0;
11         unordered_set<int> visited_stop;
12         queue<int> q;//stop
13         q.push(source);
14         visited_stop.insert(source);
15         while(!q.empty()) {
16             int sz = q.size();
17             for(int i=0; i<sz; i++) {
18                 int cur_stop = q.front();
19                 q.pop();
20                 if(cur_stop == target) return res;
21                 for(int b:stop_buses[cur_stop]) {//choose bus
22                     for(int stop:routes[b]) {//every stop in this bus route
23                         if(visited_stop.insert(stop).second) {
24                             q.push(stop);
25                         }
26                     }
27                     routes[b].clear();
28                     //one bus only to take once.
29                 }
30             }
31             res++;
32         }
33         return -1;
34     }
35 };

 

标签:target,int,Bus,stop,站点,source,bus,Routes,815
来源: https://www.cnblogs.com/habibah-chang/p/14494099.html