Bfs shortest path

#include<bits/stdc++.h>
using namespace std;

template
class graph{
int v;
map<T,list>m;

public:

graph(int v){
this->v=v;

}
void addedge(int x,int y){
m[x].push_back(y);
m[y].push_back(x);

}
void bfsshortest(T src){

 map<T,int>dist;
 bool visited[v];
 for(auto p:m){
	 dist[p.first]=-1;
    visited[p.first]=false;
 }

 queue<T>q;
 q.push(src);

dist[src]=0;
while(!q.empty()){

   T node=q.front();

   q.pop();
   visited[node]=true;
   for(auto p:m[node]){
	   if(!visited[p]){
         q.push(p);
		   dist[p]=dist[node]+6;
		   visited[p]=true;
	   }
    
   }
 }


 

//  for(auto p:m){
// 	 if(dist[p.first]==0){
// 		 cout<<"-1"<<" ";
// 	 }
// 	 else{
// 		 cout<<dist[p.first]<<" ";
// 	 }
for(int i=2;i<=v;i++){
	if(dist[i]==0){
		 cout<<"-1"<<" ";
	 }
	 else{
		 cout<<dist[i]<<" ";
	 }
}

	 
 
 cout<<endl;
 }

};

int main() {
int q;
cin>>q;
while(q–){

	int n,m;
	cin>>n>>m;
	graph<int> g(n);
	while(m--){
		int x,y;
		cin>>x>>y;
		g.addedge(x,y);
	}
	int src;
	cin>>src;
	g.bfsshortest(src);
}

return 0;

}

it gives wrong answer in all tcs

hi @kumarakash121005,
here’s your corrected and commented code https://ide.codingblocks.com/s/661179

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.