公交路线"/>
【力扣:815】公交路线
路线作为节点,广度优先
class Solution {
public:int numBusesToDestination(vector<vector<int>>& routes, int source, int target) {unordered_map<int,vector<vector<int>*>>map;queue<int>que;for(auto&& rout:routes){for(auto r:rout){map[r].push_back(&rout);}}que.emplace(source);for(int res=0;!que.empty();res++){for(int i=que.size();i;i--){int f=que.front();que.pop();if(f==target) return res;for(auto&& r:map[f]){for(int j:*r) que.emplace(j);r->clear();}}}return -1;}
};
更多推荐
【力扣:815】公交路线
发布评论