-1

Hey so I am learning BFS and I wanted to use it to know the distance from one point to another so my program scans for example

0 2 4
0 3 6
0 5 7
0 4 8
2 1 6
5 6 1
start, end, distance.

and it should tell me the distance from 0 to 1 to 2 to 3 and etcetera

so in this example, the distance from 0 to 1 is 10 because the distance from 0-2 is 4 and the distance from 2-1 is 6.

but my program when compiled says: terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc

I would like you to help me fix it so it works Thank you.

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

typedef long long ll;
typedef long double ld;
typedef vector<long long> vi;
typedef pair<long long,long long> pi;
typedef vector<pi> vpi;

#define FOR(i, a, b) for(ll i=ll(a); i<ll(b); i++)
#define ROF(i, a, b) for(ll i=ll(a); i>=ll(b); i--)
#define f first
#define s second
#define pb emplace_back
#define mp make_pair
#define SQ(a) (a)*(a)
#define all(a) (a).begin(), (a).end()

vector<pair<int,int> >adjacency_list[6];
bool visited[7];
int dist[7]={-1};
int main() {

dist[0]=0;
visited[0]=1;

for(int i=1;i<=6;i++){
  int a,b,c;
  cin>>a>>b>>c;
  adjacency_list[a].pb(mp(b,c));
    adjacency_list[b].pb(mp(a,c));
}

queue<int>q;

q.push(0);

while(!q.empty()){
  int current=q.front();
  q.pop();

for(auto i=adjacency_list[current].begin();i!=adjacency_list[current].end();i++){
if(!visited[i->first]){
  q.push(i->first);
  dist[i->first]=dist[current]+i->second;
  visited[i->first]= true;
  }
 }
}


for(int i=0;i<7;i++){
  cout<<i<<' '<<dist[i]<<endl;
}

 return 0;
}
Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
  • First off, don't use #include or using namespace std. Secondly, when you initialize dist[], keep in mind that you only initialize the first element (and not all of them) to -1. – Telescope Jun 19 '20 at 19:39
  • 1
    And remove macros. What would your ```SQ``` macro produce for ```int x = 0; SQ(x++);```? Please read this thread: https://stackoverflow.com/a/14041847/8802609 – nluk Jun 19 '20 at 19:45
  • Hey, but I don't know if you could provide some code that solves the problem since I have already tried your suggestions and I can't get it to work. – Nothingnoname Jun 19 '20 at 19:46

1 Answers1

2

adjacency_list has 6 elements but b in adjacency_list[b] can be up to 6 so your program has undefined behaviour.

I found this by replacing all the unnecessary typedefs, #defines and non standard #incldues from your code, replacing raw arrays with std::array then replacing [] with at which will detect out of bounds accesses:

#include <vector>
#include <utility>
#include <iostream>
#include <queue>
#include <array>

std::array<std::vector<std::pair<int, int> >, 6> adjacency_list;
std::array<bool, 7> visited;
std::array<int, 7> dist = { -1 };
int main() {

    dist.at(0) = 0;
    visited.at(0) = 1;

    for (int i = 1; i <= 6; i++) {
        int a, b, c;
        std::cin >> a >> b >> c;
        adjacency_list.at(a).emplace_back(b, c);
        adjacency_list.at(b).emplace_back(a, c);
    }

    std::queue<int>q;

    q.push(0);

    while (!q.empty()) {
        int current = q.front();
        q.pop();

        for (auto i = adjacency_list.at(current).begin(); i != adjacency_list.at(current).end(); i++) {
            if (!visited.at(i->first)) {
                q.push(i->first);
                dist.at(i->first) = dist.at(current) + i->second;
                visited.at(i->first) = true;
            }
        }
    }

    for (int i = 0; i < 7; i++) {
        std::cout << i << ' ' << dist.at(i) << "\n";
    }

    return 0;
}
Alan Birtles
  • 32,622
  • 4
  • 31
  • 60