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;
}
Aucun commentaire:
Enregistrer un commentaire