C++ Dijkstra Алгоритм Дейкстры не работает на основе списка stl
Pastebin - https://pastebin.com/hKNgL0xR Программа выводит какой-то бред, будто он не работает, но при этом условия выполняются. Помогите пожалуйста исправить эту проблему, уже час не виду в чем проблема... КОД
#include <iomanip>
#include <vector>
#include <algorithm>
const double INF = 9999;
const int COLORS = 3;
using namespace std;
inline double drand(double d, double u) { return d + (static_cast<double>(rand()) / RAND_MAX) * (u - d); }
template <class T1, class T2> class Graph {
struct edge {
T2 w;
T1 u, v;
edge(T2 w, T1 u, T1 v) : w(w), u(u), v(v) {}
bool operator<(const edge& a) const { return w < a.w; }
};
T1 V;
vector<edge>* adjacencyList, edges;
public:
vector<edge>* getAdjacencyList() { return adjacencyList; }
// A constructor that builds a graph according to the given parameters of the number of
// vertices, density and range
Graph(T1 V = 0, T2 density = 0, T2 dnLim = 0, T2 upLim = 0) : V(V), adjacencyList(new vector<edge>[V]) {
if (dnLim > upLim) swap(dnLim, upLim);
for (T1 i = 0; i < V; i++)
for (T1 e = 0; e < V * min(static_cast<T2>(1), max(static_cast<T2>(0), density)); e++)
addEdge({ static_cast<T1>(drand(dnLim, upLim)), i, static_cast<T1>(rand() % V) });
}
// Add an edge in an undirected graph, return true if edge was added succesfully
bool addEdge(edge e) {
if (e.u == e.v) return false; // No loops in this graph
for (const edge& way : adjacencyList[e.u]) if (way.u == e.v) return false; // No dublicates in this graph
adjacencyList[e.u].push_back(e);
adjacencyList[e.v].push_back(e);
edges.push_back(e);
return true;
}
// Print the adjacency list representation of graph
void printAdjacencyList(vector<edge>* al = nullptr) {
if (al == nullptr) al = adjacencyList;
T1 w = static_cast<T1>(4);
for (T1 v = 0; v < V; v++) {
cout << "Vertex " << setw(w) << v << ':';
for (auto& e : al[v])
cout << " -> " << setw(w) << e.u << ":w=" << fixed << setprecision(3) << e.w;
cout << endl;
}
}
// Minimum distance among unvisited vertices
T1 minDistance(T2* dist, bool* visited) {
T2 min = INF;
T1 index = 0;
for (T1 i = 0; i < V; i++)
if (!visited[i] && dist[i] < min) min = dist[i], index = i;
return index;
}
// Typical representation of Dijkstra's algorithm (breadth search)
pair<T2*, T1*> dijkstra(T1 source, bool r = true, bool g = true, bool b = true) {
T2* dist = new T2[V];
T1* prev = new T1[V];
bool* visited = new bool[V];
for (T1 i = 0; i < V; i++) dist[i] = INF, prev[i] = -1, visited[i] = false;
dist[source] = 0, prev[source] = source;
for (T1 i = 0; i < V - 1; i++) {
T1 nearest = minDistance(dist, visited);
visited[nearest] = true;
for (edge adj : adjacencyList[nearest])
if (!visited[adj.v] && dist[nearest] != INF && dist[nearest] + adj.w < dist[adj.v])
prev[adj.v] = nearest, dist[adj.v] = dist[nearest] + adj.w;
}
printAdjacencyList(adjacencyList);
cout << "PREV:";
for (T1 i = 0; i < V - 1; i++) cout << ' ' << prev[i];
cout << endl;
return make_pair(dist, prev);
}
};
// The function of calculating the arithmetic mean of only positive numbers not equal
// to infinity, taking into account their number in the array
template <class T1, class T2> T2 calcAveragePositiveDistance(pair<T2*, T1*> p, T1 size) {
if (size == 0) return 0;
T2 positiveSum = 0;
T1 positiveCount = size;
for (T1 i = 0; i < size; i++)
if (p.second[i] >= 0 && p.first[i] < INF) positiveSum += p.first[i];
else positiveCount--;
return positiveSum / positiveCount;
}
int main() {
short V; cout << "Input V: "; cin >> V; if (V < 0) V = abs(V);
double density; cout << "Input density (from 0 to 1): "; cin >> density;
double dnLim; cout << "Input dnLim for random: "; cin >> dnLim;
double upLim; cout << "Input upLim for random: "; cin >> upLim;
int source; cout << "Input source vertice (from 0 to " << V - 1 << "): "; cin >> source;
Graph<short, short> graph(V, density, dnLim, upLim);
pair<short*, short*> dijkstraInfo = graph.dijkstra(source);
double avgDist = calcAveragePositiveDistance(dijkstraInfo, V);
// The output of all the necessary information obtained during the operation of Dijkstra's algorithm
for (int i = 0; i < V; i++)
cout << source << "\t->\t"
<< i << "\tMin dist:\t" << setprecision(5) << dijkstraInfo.first[i]
<< "\tV Prev:\t" << dijkstraInfo.second[i] << endl;
cout << "AVERAGE MIN POSITIVE DISTANCE: " << setprecision(5) << avgDist << endl;
return 0;
}