Vectors of vectors question

Is there a way to jump to from one index to another

Example vector< vector <int> >v:

1 2 4
2 5 
3 1 6
4 
5 2 1
6 4


I iterate through the first vector and start at 1. I read '2' and then I go to index 2, read '3' go to index '5' go to index 5, read '2' and go to index 2. I'm wondering if this is possible.
There has to be a defined basis for the jumps – for e.g you start off on a row, if it has a second element read that element (else what?) –then, if the 2d vector has a row corresponding to this second element then go to the start of this row (again, else what?) and so on – rather than spitting out numbers one after the other you should describe the algorithm you have in mind in words (including the if-else scenarios)
My algorithm is pretty much dijkstra's algorithm. Next to each column except the first one should have a weight attached to it. I want to compare the weight of all the possible paths from A to B.

Example shortest path from 1 to 4:

1 2 3.2 4 4.1
2 1 2.1 3 2.2
3 4 2
4


What I was planning to do was go to v[0] (which is 1) and then check v[0][0] save the weight which is '3.2' then go to v[1]. Check v[1][0] adds the weigh to total, which is '5.3' and then go back to v[0]. Check the v[0][1] since I will have a bool to check if the path was already taken and it'll go to 4 with total weight '9.4'. That would be the first shortest path. My algorithm will run again, and this time will check v[0][1] and it jumps to 4 with the total of '4.1.' So it'll output path taken 1 4 (4.1). I short of have an idea what I should be doing. Create a for loop with the size of the second vector so that it'll check every element, but the part I'm struggling with is jumping from v[0] to v[3].

Edit: Because of the way I was thinking of approaching this problem, After reading '2' from index '1' it will check both elements inside v[1], so it'll be 1,2,1,4 & 1,2,3,4
Last edited on
Topic archived. No new replies allowed.