Untitled

mail@pastecode.io avatar
unknown
c_cpp
6 months ago
766 B
3
Indexable
Never
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;
    }
};
Leave a Comment