An undirected graph with N vertices and M edges is specified. Vertices are numbered starting from one. Each edge is characterized by its weight and color (red or black). You need to find a path with a minimum total weight from vertex 1 to vertex N. In this case, the path must be interspersed along red and black edges. This means that if the first edge in the path found is red, then the second must be black, the third must be red again, and so on. If the first edge is black, then the second must be red, and so on.
I tried, solved and turned out some algorithm, but it does not always work correctly. Maybe someone will have ideas?
I tried the Dijkstra algorithm. At the same time, for each vertex I kept the color of the edge that I hit on this vertex and then if I can get to this vertex by the red and black edges, then I checked this vertex 2 times. And then I just went through all the vertices in which you can get out of it, and so for each.
It did not work out what exactly I do not know. Since, in my case, the test goes in the following way: there are 27 tests (which I don’t see). And now I have 20/27. Errors in the code I can not find the code itself
versh.push(0); reddist[0] = 0; blackdist[0] = 0; while (!versh.empty()) { int temp; temp = versh.front(); versh.pop(); for (int l = 0; l < N; l++) { int m = 1; if (ves[temp][l] >= 0) { if (temp != 0) { if (redcolor[temp] != -1) { if (cvet[temp][l] != 0) { blackcolor[l] = cvet[temp][l]; m = 2; } }//цвет вершины в которую пришёл по чёрному чёрный if (blackcolor[temp] != -1) { if (cvet[temp][l] != 1) { redcolor[l] = cvet[temp][l]; m = 3; } }//аналогично только красный } else { if (cvet[temp][l] == 0) { redcolor[l] = 0; m = 3; } else { blackcolor[l] = 1; m = 2; } } if (redcolor[l] != -1 && m == 3) { if (reddist[l] > blackdist[temp] + ves[temp][l]) { reddist[l] = blackdist[temp] + ves[temp][l]; versh.push(l); if (l == N - 1) { if (res > reddist[l]) res = reddist[l]; } } } if (blackcolor[l] != -1 && m == 2) { if (blackdist[l] > reddist[temp] + ves[temp][l]) { blackdist[l] = reddist[temp] + ves[temp][l]; versh.push(l); if (l == N - 1) { if (res > blackdist[l]) res = blackdist[l]; } } } } m = 1; } }