Untitled
unknown
c_cpp
2 years ago
766 B
7
Indexable
class Solution {
public:
vector<int> shortestPath(vector<vector<int>>& edges, int N,int M, int src){
vector<int> vis(N,0),dist(N,-1);
dist[src]=0;
unordered_map<int,vector<int>> m;
for(auto v:edges)
{
m[v[0]].push_back(v[1]);
}
queue<pair<int,int>>q;
q.push({src,0});
vis[src]=1;
while(q.size())
{
int t=q.front().first;
int d=q.front().second;
q.pop();
for(auto i:m[t])
{
if(!vis[i])
{
vis[i]=1;
q.push({i,d+1});
dist[i]=d+1;
}
}
}
return dist;
}
};Editor is loading...
Leave a Comment