Создание Dijkstra в C ++ - Проблемы итератора приоритетных очередейC++

Программы на C++. Форум разработчиков
Ответить Пред. темаСлед. тема
Anonymous
 Создание Dijkstra в C ++ - Проблемы итератора приоритетных очередей

Сообщение Anonymous »

Я только что выучил Дейкстра в школе, но я хотел попытаться кодировать это. У меня проблема с итератором и меняю определенную точку в векторе, но, скорее всего, моя реализация неверна. Ошибка, которую я получаю ниже, и мне интересно, как я изменяю std :: pair на объект std :: _ vector_iterator. Кроме того, что означает _SCARY_VAL? *") существует < /p>
my header file < /p>

Код: Выделить всё

#pragma once
#include 
#include 
class Node;
class Graph;
class Edge;

class Edge {
public:
Node* from;
Node* to;
int weight;
//bool directional;
Edge() = default;

Edge(Node* to, Node* from, int weight, bool directional);
};

class Node {
public:
Node() = default;
Node(Graph& graph, std::string name);
std::vector conncections;
std::string name;
void printConnections();

};

class Graph {
public:
Graph() = default;

std::vector GraphNodes;

void printGraph();
};

void dijkstra(Node* start, Node* end);
< /code>
my cpp -файл.  < /p>
#include "Graphs.h"
#include 
#include 

Graph graph1;
Node A(graph1, "A");
Node B(graph1, "B");
Node C(graph1, "C");
Node D(graph1, "D");
Node E(graph1, "E");
Node F(graph1, "F");
Node G(graph1, "G");
Node H(graph1, "H");
Node I(graph1, "I");
Node J(graph1, "J");

Node* Aptr = &A;
Node* Bptr = &B;
Node* Cptr = &C;
Node* Dptr = &D;
Node* Eptr = &E;
Node* Fptr = &F;
Node* Gptr = &G;
Node* Hptr = &H;
Node* Iptr = &I;
Node* Jptr = &J;

//A C B D E F G H I
Edge test1(Aptr, Bptr, 50, false);
Edge test2(Aptr, Cptr, 25, false);
Edge test3(Cptr, Eptr, 45, false);
Edge test4(Cptr, Fptr, 50, false);
Edge test5(Bptr, Iptr, 80, false);
Edge test6(Bptr, Dptr, 25, false);
Edge test7(Dptr, Iptr, 70, false);
Edge test8(Dptr, Fptr, 10, false);
Edge test9(Eptr, Gptr, 30, false);
Edge test10(Eptr, Hptr, 35, false);
Edge test11(Gptr, Jptr, 80, false);
Edge test12(Iptr, Jptr, 30, false);

using std::cout;

Edge::Edge(Node* from, Node* to, int weight, bool directional) {
this->to = to;
this->from = from;
this->weight = weight;

from->conncections.push_back(this);

if (!(directional)) {
/*new Edge(to, from, weight, true);*/
}
}

Node::Node(Graph& graph, std::string name) {
graph.GraphNodes.push_back(this);
this->name = name;
}

void Graph::printGraph() { //this will call Node::printConnections
for (auto& Node : this->GraphNodes) {
Node->printConnections();
}
}

void Node::printConnections() {
for (auto& Node : this->conncections) {
cout name name conncections) { //goes through connections which hold edges for that node

if (std::find(visited.begin(), visited.end(), node->to) != visited.end())
continue; //this is checking if the node is already visited

auto temp = std::make_pair(node->to, node->weight + current.second);
int i = 0;
while (i != notVisited.size() && notVisited[i].second < temp.second) {
i++;
}
notVisited.insert(notVisited.begin() + i, temp); //inserts the element in the right place
visited.push_back(node->from); //adds it to visited

auto it = (std::find(visited.begin(), visited.end(), node->to));

if (*it != node->to)
finalWeight.emplace_back(temp);
else {
*it = temp;
}

}
}
for (int i = 0; i < finalWeight.size(); i++) {
cout name 

Подробнее здесь: [url]https://stackoverflow.com/questions/79693159/making-dijkstras-in-c-priority-queue-iterator-problems[/url]
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «C++»