-1

I am trying to do Breadth First Search to find the shortest bath on graph the Weights to all nodes is 6 this is my code :

#include <bits/stdc++.h>
#define pb push_back
using namespace std;
bool vis[1200];
int dist[1200];
vector< int > edges[1200];
int main()
{
    int q ;
    cin>>q;
    while(q--)
    {
        int n,m;
        cin>>n>>m;
        for(int i=0; i<m; i++)
        {
            int u,v;
            cin>>u>>v;
            edges[u].pb(v);
            edges[v].pb(u);
        }
        int s;
        cin>>s;
        queue <int> q;
        q.push(s);
        vis[s]=true;
        while(!q.empty())
        {
            int node=q.front();
            q.pop();
            for(int i=0; i<edges[node].size(); i++)
            {
                if(!vis[edges[node][i]])
                {
                    dist[edges[node][i]]=dist[node]+6;
                    q.push(edges[node][i]);
                }
            }
        }
        for(int i=1;i<=n;i++){
            if(i==s)
                continue;
            if(dist[i]==0)
                cout<<-1<<' ';
            else
                cout<<dist[i]<<' ';
        }
        memset(vis,0,sizeof vis);
        memset(dist,0,sizeof dist);
        for(int i=0;i<1200;i++){
            edges[i].clear();
        }
        cout<<endl;
    }
    return 0;
}

's' is the start node if any node i cant reach then print -1 'q' is the number of queries 'm' is the number of edges 'n' is the number of nodes

Yazan
  • 3
  • 1
  • Have you tried stepping trough your code with a debugger to see what actually happens? – MivVG Mar 24 '18 at 10:54
  • 4
    First please read [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) Then please [learn how to debug your programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Some programmer dude Mar 24 '18 at 10:54
  • 3
    If this `#define pb push_back` is what that _competitive programming_ site is teaching you then you are well advised to stay away from it and read these [C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) instead. – Ron Mar 24 '18 at 10:55
  • i am using codeblocks and the debugger didnt work – Yazan Mar 24 '18 at 11:00
  • then fix your debugger first – deW1 Mar 24 '18 at 11:02
  • you have array of 1200 `vector< int >` ... did you mean a vector of 1200 elements? And note, `vector< int > edges (1200);` isn't that, it's a vector of one element with value 1200. – Swift - Friday Pie Mar 24 '18 at 11:32
  • i mean array of 1200 vector – Yazan Mar 24 '18 at 11:35

1 Answers1

0

Mark popped node as visited, this will solve your runtime error.

#include <bits/stdc++.h>
#define pb push_back
using namespace std;
bool vis[1200];
int dist[1200];
vector< int > edges[1200];
int main()
{
    int q ;
    cin>>q;
    while(q--)
    {
        int n,m;
        cin>>n>>m;
        for(int i=0; i<m; i++)
        {
            int u,v;
            cin>>u>>v;
            edges[u].pb(v);
            edges[v].pb(u);
        }
        int s;
        cin>>s;
        queue <int> q;
        q.push(s);
        vis[s]=true;
        while(!q.empty())
        {
            int node=q.front();
            q.pop();
            // mark this popped node as visited 
            vis[node] = true;
            for(int i=0; i<edges[node].size(); i++)
            {
                if(!vis[edges[node][i]])
                {
                    dist[edges[node][i]]=dist[node]+6;
                    q.push(edges[node][i]);
                }
            }
        }
        for(int i=1;i<=n;i++){
            if(i==s)
                continue;
            if(dist[i]==0)
                cout<<-1<<' ';
            else
                cout<<dist[i]<<' ';
        }
        memset(vis,0,sizeof vis);
        memset(dist,0,sizeof dist);
        for(int i=0;i<1200;i++){
            edges[i].clear();
        }
        cout<<endl;
    }
    return 0;
}