Question: #include #include #include #include #include #include #include #include using namespace std; class Graph { private: int nodeCount; vector > > adjacencyList; vector prevNode; public: Graph

#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
class Graph {
private:
int nodeCount;
vector>> adjacencyList;
vector prevNode;
public:
Graph(int nodeCount);
void insertEdge(int start, int end, int weight);
vector runDijkstra(int source);
void printDistances(const vector& distances);
};
Graph::Graph(int nodeCount){
this->nodeCount = nodeCount;
adjacencyList.resize(nodeCount);
prevNode.resize(nodeCount,-1);
}
void Graph::insertEdge(int start, int end, int weight){
adjacencyList[start].emplace_back(end, weight);
adjacencyList[end].emplace_back(start, weight); // Assuming undirected graph
}
vector Graph::runDijkstra(int source){
vector distance(nodeCount, INT_MAX);
priority_queue, vector>, greater>> pq;
distance[source]=0;
pq.emplace(0, source);
while (!pq.empty()){
int currentNode = pq.top().second;
int currentDistance = pq.top().first;
pq.pop();
for (const auto& neighbor : adjacencyList[currentNode]){
int nextNode = neighbor.first;
int edgeWeight = neighbor.second;
if (distance[nextNode]> currentDistance + edgeWeight){
distance[nextNode]= currentDistance + edgeWeight;
prevNode[nextNode]= currentNode;
pq.emplace(distance[nextNode], nextNode);
}
}
}
return distance;
}
void Graph::printDistances(const vector& distances){
for (int dist : distances){
if (dist == INT_MAX){
cout << "INF";
} else {
cout << setw(3)<< right << dist;
}
cout <<""; // Three spaces between each number
}
cout << endl;
}
int main(){
ifstream inputFile("input1.txt");
if (!inputFile.is_open()){
cerr << "Error opening file." << endl;
return 1;
}
int nodeCount;
inputFile >> nodeCount;
Graph graph(nodeCount);
int startNode, endNode, edgeWeight;
while (inputFile >> startNode >> endNode >> edgeWeight){
graph.insertEdge(startNode, endNode, edgeWeight);
}
inputFile.close();
ofstream outputFile1("output1.txt");
ofstream outputFile2("output2.txt");
ofstream outputFile3("output3.txt");
if (!outputFile1.is_open()||!outputFile2.is_open()||!outputFile3.is_open()){
cerr << "Error opening output files." << endl;
return 1;
}
for (int i =0; i < nodeCount; ++i){
vector distances = graph.runDijkstra(i);
graph.printDistances(distances);
}
outputFile1.close();
outputFile2.close();
outputFile3.close();
return 0;
} with this code im getting this output:
04121921119814
408152212121110
12807144672
19157091113149
2122149010121316
1112411100236
912613122016
811714133107
141029166670 when the formatting should be as such:04121921119814
408152212121110
12807144672
19157091113149
2122149010121316
1112411100236
912613122016
811714133107
141029166670

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!