Загрузка данных


#include "task5_NN.h"

Graph::Graph(int v) : vertices(v) {
    adjMatrix.resize(vertices, vector<int>(vertices, numeric_limits<int>::max()));
    for (int i = 0; i < vertices; i++) {
        adjMatrix[i][i] = 0;
    }
}

void Graph::loadFromFile(const string& filename) {
    ifstream file(filename);
    if (!file.is_open()) {
        cout << "Файл не может быть открыт" << endl;
    }
    for (int i = 0; i < vertices; i++) {
        for (int j = 0; j < vertices; j++) {
            file >> adjMatrix[i][j];
        }
    }
    file.close();
}

void Graph::printGraph(ostream& os) {
    for (const auto& row : adjMatrix) {
        for (const auto& weight : row) {
            if (weight == numeric_limits<int>::max()) {
                os << "INF";
            }
            else {
                os << weight << " ";
            }
        }
        cout << endl;
    }
}

void Graph::dijkstra(int startVertex, const string& outputFile) {
    vector <int> distances(vertices, numeric_limits<int>::max());
    distances[startVertex] = 0;
    using Pair = pair<int, int>;
    priority_queue<Pair, vector<Pair>, greater<Pair>> pq;
    pq.push({ 0, startVertex });
    while (!pq.empty()) {
        int currentVertex = pq.top().second;
        pq.pop();

        for (int i = 0; i < vertices; i++) {
            if (adjMatrix[currentVertex][i] != numeric_limits<int>::max()) {
                int newDistance = distances[currentVertex] + adjMatrix[currentVertex][i];
                if (newDistance < distances[i]) {
                    distances[i] = newDistance;
                    pq.push({ newDistance, i });
                }
            }
        }
    }
    ofstream outFile(outputFile);
    for (int i = 0; i < vertices; i++) {
        outFile << "Расстояние до вершины " << i << ": ";
        if (distances[i] == numeric_limits<int>::max()) {
            outFile << "INF" << endl;
        }
        else {
            outFile << distances[i] << endl;
        }
    }
    outFile.close();
}

---------------------------

#pragma once
#include <iostream>
#include <vector>
#include <fstream>
#include <limits>
#include <queue>
#include <utility>
#include <string>

using namespace std;

class Graph {
private:
	int vertices;
	vector<vector<int>> abjMatrix;
public:
	Graph(int v);
	void loadFromFile(const string& filename);
	void printGraph(ostream& os);
	void dijkstra(int startVertex, const string& outputFile);
};


----------------------



#include <task5_NN>

int main(int argc, char* argv[]) {
	if (argc != 3) {
		cout << "ERROR" << endl;
		return 1;
	}

	int numberOfVertices = stoi(argv[1]);
	string inputFilename = argv[2];

	Graph graph(numberOfVertices);
	graph.loadFromFile(inputFilename);
	graph.printGraph(cout);

	int startVertex = 0;
	graph.dijkstra(startVertex, "result.txt");

	return 0;
};