#include #include main(int argc, char *argv[]) { //declaration of the variables. float mtx1[20][20], mtx2[20][20], mtx3[20][20]; float min, max; int n, i, j, k; //exit if the number of the arguments is not 1. if(argc != 2) { cerr << "Usage: dist \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 >> mtx1[i][j]; //read the matrix elements. } } fin.close(); //close the file 1. for(j=1; j<=n; j++) { for(i=1; i<=n; i++) { mtx2[i][j] = mtx1[i][j]; //copy the matrix 1 to matrix 2. } } while(1) { for(j=1; j<=n; j++) { for(i=1; i<=n; i++) { mtx3[i][j] = 0; //set the matrix 3 as zero. } } for(j=1; j<=n; j++) { for(i=1; i<=n; i++) { min = 9999; for(k=1; k<=n; k++) { if(min > mtx2[i][k] + mtx1[k][j]) { min = mtx2[i][k] + mtx1[k][j]; //calculation of the shortest path. } } mtx3[i][j] = min; } } max = 0; for(j=1; j<=n; j++) { for(i=1; i<=n; i++) { if(mtx3[i][j] > max) { max = mtx3[i][j]; //maximum of the elements of the matrix 3. } } } if(max < 9999) break; //exit if there are no "9999". for(j=1; j<=n; j++) { for(i=1; i<=n; i++) { mtx2[i][j] = mtx3[i][j]; //copy the matrix 3 to matrix 2. } } } //output the results. cout << n << "\n"; for(j=1; j<=n; j++) { for(i=1; i<=n; i++) { cout << mtx3[i][j]; if(i < n) { cout << "\t"; } } cout << "\n"; } return 0; } //end of the program.