We are solving BFS shortest path problem by using of BFS shortest path method...but why my answers is wrong during run time...even compilation is successfull

class graph
{
list*l;
ll int n;
public:
graph(ll int n)
{
this->n=n;
l=new list[n+1];
}
void add_edge(ll int v,ll int u)
{
l[v].push_back(u);
}
void short_path(int s)
{
ll int dist[n+1];
for(ll int i=1;i<=n;i++)
dist[i]=INT_MAX;
dist[s]=0;
queueq;
q.push(s);

		while(!q.empty())
		{
			int node=q.front();
			q.pop();
			list<ll int>::iterator it;
			for(it=l[node].begin();it!=l[node].end();++it)
			{
				if(dist[*it]==INT_MAX)
				{
				dist[*it]=dist[node]+1;
				q.push(*it);
				}
			}
		}
		/*for(int i=1;i<=n;i++)
		cout<<i<<"-->"<<dist[i]<<endl;*/
		for(ll int i=1;i<=n;i++)
		{
			if(i!=s&&dist[i]!=INT_MAX)
			cout<<dist[i]*6<<" ";
			else if(i!=s&&dist[i]==INT_MAX)
			cout<<"-1"<<" ";
		}
}

};
int main()
{
ll int t;
cin>>t;
while(t–)
{
ll int n,m;
cin>>n>>m;
graph g(n);
while(m–)
{
ll int v,u;
cin>>v>>u;
g.add_edge(v,u);
}
ll int s;
cin>>s;
g.short_path(s);
cout<<endl;

}
return 0;

}

hi @tusharkhandelwal315, you are given a undirected graph but you are taking input as a directed graph i.e.in your add_edge() function add l[u].push_back(v);
i have corrected your code : -

In case of any doubt feel free to ask :slight_smile:
mark your doubt as resolved If you got the answer

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.