#include <iostream> #include <fstream> #include <vector> #include <sstream> using…
- User
#include #include #include #include
using namespace std;
const int INF = 1e9; // Assuming INF represents infinity
class floydGraphProcessor { public: void processInput(const string &line, ifstream &infile, ofstream &outfile) { // Extract problem number and matrix size size_t problem_position = line.find("Problem"); if (problem_position == string::npos) { cerr << "Error extracting parameters. \n"; return; }
codeint problemNumber, n; sscanf(line.c_str() + problem_position, "Problem%d", &problemNumber); // Extracting n from the line size_t n_position = line.find("n = "); if (n_position == string::npos) { cerr << "Error: 'n =' not found.\n"; return; } sscanf(line.c_str() + n_position, "n = %d", &n); // Resize output matrix to n x n vector<vector<int>> matrix(n, vector<int>(n)); // Read n x n matrix values for (int i = 0; i < n; i++) { string r; getline(infile, r); istringstream iss(r); for (int j = 0; j < n; j++) { iss >> matrix[i][j]; } } // Output to file outfile << "Problem " << problemNumber << ": n = " << n << endl; vector<vector<int>> dist = matrix; vector<vector<int>> P(n, vector<int>(n, 0)); // Floyd-Warshall algorithm for (int k = 0; k < n; k++) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (k != i && k != j && (dist[i][j] >= dist[i][k] + dist[k][j])) { dist[i][j] = dist[i][k] + dist[k][j]; P[i][j] = k + 1; } } } } // Output final Pmatrix to file outfile << "Pmatrix:" << endl; printPath(P, n, outfile); outfile << endl; // shortest paths and length to file for (int i = 0; i < n; i++) { outfile << "V" << i + 1 << "-Vj: shortest path and length" << endl; for (int j = 0; j < n; j++) { outfile << "V" << i + 1 << " "; if (i != j) { outfile << "V" << j + 1 << " "; int v = j; while (P[i][v] != 0) { outfile << "V" << P[i][v] << " "; v = P[i][v] - 1; } } outfile << ": " << dist[i][j] << endl; } } } - Assistant