Алгоритм Дейкстры нахождения кратчайшего пути

Граф в задаче неориентированный. Как выключить некоторые рёбра, чтобы пользователь сам выбирал какие ему рёбра не нужны в данный момент?

Под числом с плавающей запятой подразумевается сумма чисел полученная по итогу выполнения алгоритма.

#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    vector<int> parent(5, -1); // объявили и сразу заполнили -1-цами в кол-ве 5 штук
    int big_num(10000);
    int matrix[5][5] = { {0,5,12,0,0},
    {5,0,4,0,1},
    {12,4,0,2,0},
    {0,0,2,0,5},
    {0,1,0,5,0} };

    int pos[5], node[5], min(0), index_min(0);
    for (int i = 0; i < 5; ++i) {
        pos[i] = big_num;
        node[i] = 0;
    }
    for (int i = 0; i < 5; ++i) {
        for (int j = 0; j < 5; ++j) {
            cout << setw(4) << matrix[i][j];
        }
        cout << "\n";
    }
    int x;

    cout << "vvedite x1";

        cin >> x;
    pos[x - 1] = 0; // начальная точка 
    cout << "\n";
    for (int i = 0; i < 4; ++i) {
        min = big_num;
        for (int j = 0; j < 5; ++j) {
            if (!node[j] && pos[j] < min) {
                min = pos[j];
                index_min = j;
            }
        }
        node[index_min] = true;
        for (int j = 0; j < 5; ++j) {
            if (!node[j] && matrix[index_min][j] > 0 && pos[index_min] != big_num && pos[index_min] + matrix[index_min][j] < pos[j]) {
                pos[j] = pos[index_min] + matrix[index_min][j];
                parent.at(j) = index_min; // запоминаем предка вершины j
            }
        }
    }
    int n(0);
    cout << "\nnumber of top? : ";
    cin >> n;

    vector<int>temp; // n - 1, так как не забываем про индексацию
    for (int i = n - 1; i != -1; i = parent.at(i))temp.push_back(i + 1); // а все что здесь делается описано в справке,которую я те кинул
    reverse(temp.begin(), temp.end());
    for (int i = 0; i < temp.size(); ++i)cout << temp.at(i) << " ";

    cout << "\nWeight : " << pos[n - 1] << "\n";

    cout << endl;
    return 0;
}


Ответы (0 шт):