我在编写程序后尝试调试我的程序,但遇到了这个错误:
这是我的代码:
#include<fstream>
#include <iostream>
#include <vector>
#include <string>
#define MAXINT 2147483647
using namespace std;
struct Edge
{
int id, weight;
Edge(int y, int w)
{
id = y;
weight = w;
}
};
struct Node
{
vector <Edge *> Edges;
};
struct Graph
{
vector < Node *> vertices;
vector < int > indices;
void Resize(int x)
{
if (vertices.capacity() < x)
vertices.resize(x);
}
void InsertEdge(int x, int y, int weight)
{
Resize(((x > y) ? x : y) + 1);
InsertVertex(x);
InsertVertex(y);
vertices[x]->Edges.push_back(new Edge(y, weight));
}
void InsertVertex(int x)
{
if (vertices[x] == NULL)
{
Node *t = new Node;
vertices[x] = t;
indices.push_back(x);
}
}
};
void Dij(Graph const &g, int start)
{
Node *temp;
vector<bool> check;
vector<int> distance, prev;
int v, w, weight, dist;
for (int i = 0; i <= g.indices.size(); i++)
{
check.push_back(false);
distance.push_back(MAXINT);
prev.push_back(-1);
}
v = start;
distance[v] = 0;
while (!check[v])
{
check[v] = true;
temp = g.vertices[v];
for (int i = 0; i < temp->Edges.size(); i++)
{
w = temp->Edges[i]->id;
weight = temp->Edges[i]->weight;
if (distance[w] > (distance[v] + weight))
{
distance[w] = distance[v] + weight;
prev[w] = v;
}
}
v = 1;
dist = MAXINT;
for (int x = 0; x < g.indices.size(); x++)
{
int i = g.indices[x];
if (!check[i] && dist > distance[i])
{
dist = distance[i];
v = i;
}
}
}
}
int main()
{
int startNode, nodeOne, nodeTwo, number;
Graph g;
ifstream myReadFile;
myReadFile.open("P:\\Documents\\New Folder\\Test\\src\\Read.txt");
while (!myReadFile.eof())
{
myReadFile >> nodeOne;
myReadFile >> nodeTwo;
myReadFile >> number;
g.InsertEdge(nodeOne, nodeTwo, number);
}
cout<< "Enter the starting node: ";
cin >> startNode;
Dij(g, startNode);
return 0;
}
我为烦人的格式道歉=/。它在 dij 方法的最后一个 for 循环中中断。有人知道我可能会省略什么吗?