//Floyd-Warshall algorithm //program name "fw.cpp" #include #include #include #include using namespace std; int main(int argc, char *argv[]) { //declaration of the variables. float mtx[20][20]; int n, i, j, k; //exit if the number of the arguments is not 1. if (argc != 2) { cerr << "Usage: fw \n"; return 1; } //exit if the imput file cannot be opend. ifstream fin(argv[1]); if (!fin) { cerr << "Can't open file!\n"; return 1; } //read the input file. fin >> n; //read the number of rows and columns. for (j = 1; j <= n; j++) { for (i = 1; i <= n; i++) { fin >> mtx[i][j]; //read the matrix elements. } } fin.close(); //close the file 1. for (k = 1; k <= n; k++) { for (j = 1; j <= n; j++) { for (i = 1; i <= n; i++) { if (mtx[i][j] > mtx[i][k] + mtx[k][j]) { mtx[i][j] = mtx[i][k] + mtx[k][j]; } } } } //output the results. cout << n << "\n"; for (j = 1; j <= n; j++) { for (i = 1; i <= n; i++) { cout << mtx[i][j]; if (i < n) { cout << "\t"; } } cout << "\n"; } return 0; } //end of the program.