1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
#include <iostream>
#include <deque>
#include <vector>
#define INT_MAX 21422
using namespace std;
int distancee[10]={4,4,4,4,4,4,4,4,4,4}; //want to intialize all to infinity
struct bfss{
int firstt;
int secondd;
};
vector<bfss>bfs[10];
void bfsfunc(int start);
int main()
{
int edges,nodes,x,y,z;
cin>>edges>>nodes;
for(int i=0;i<edges;i++){
cin>>x>>y>>z; //x is array subscript , y is node(x-y is edge) , z is weight
bfss newbfs;
newbfs.firstt=y;
newbfs.secondd=z;
bfs[x].push_back(newbfs);
bfss newbfs;
newbfs.firstt=x;
newbfs.secondd=z;
bfs[y].push_back(newbfs); // when making undirected graph, should i push back 2 times?
}
bfsfunc(0);
return 0;
}
void bfsfunc(int start){
deque<int> q;
q.push_back(start);
distancee[start]=0;
while(!q.empty()){
int v=q.front();
q.pop_front();
for(int i=0;i<bfs[v].size();i++){
if(distance[bfs[v][i].firstt]>(distance[v]+bfs[v][i].secondd)){ // got error in this line
distance[bfs[v][i].firstt]=distance[v]+bfs[v][i].secondd;
if(bfs[v][i].second==0)
{
q.push_front(bfs[v][i].first);
}else{
q.push_back(bfs[v][i].second);
}
}
}
}
}
|