You should build a new graph, that is constructed the the way trincot described in his answer to you previous post and work only on this single graph. This makes it easier to implement dijkstra, because you have to handle the "go to other graph" case.
So you basically copy the graph 3 times, but each time, you rename the vertices slightly different. This way you can easily add the links in this new graph.
for(auto vertex : graph){
// init newgraph[vertex]
for(auto edge : vertex){
// edge is just the other vertex of this edge
newgraph[vertex].push_back(edge); //just copy
newcosts[vertex][edge] = costs[vertex][edge]; // just copy
}
if(colorof(vertex) == "red")
newgraph[vertex].push_back(vertex + "_");
newcosts[vertex][edge + "_"] = 0;
}
for(auto vertex : graph){
// init newgraph[vertex+"_"]
for(auto edge : vertex){
newgraph[vertex + "_" ].push_back(edge + "_" ); //just copy
newcosts[vertex + "_" ][edge + "_" ] = costs[vertex][edge]; // just copy
}
if(colorof(vertex) == "blue")
newgraph[vertex + "_" ].push_back(vertex + "__");
newcosts[vertex + "__"][edge] = 0;
}
for(auto vertex : graph){
// init newgraph[vertex+"_"]
for(auto edge : vertex){
newgraph[vertex + "__" ].push_back(edge + "__" ); //just copy
newcosts[vertex + "__" ][edge + "__" ] = costs[vertex][edge]; // just copy
}
}
Where graph
contains you graph, costs[a][b]
contains the costs from vertex a
to b
.
I used strings as names for vertices to make naming easier, you can store them in a map if you like or you use integers and vectors for higher speed.
For dijkstra you have to store the length in the "middle" graph in addition to the normal path length for every vertex. You do this the same way, but you only increase the length, when both vertices are in the "middle" graph. and you only update the vertex, when you condition for the middle graph is true.