Pagini recente » Cod sursa (job #1522780) | Cod sursa (job #946366) | Cod sursa (job #2047896) | Cod sursa (job #707575) | Cod sursa (job #2763460)
#include <fstream>
using namespace std;
ifstream fin("royfloyd.in");
ofstream fout("royfloyd.out");
int w[110][110], n;
int floydwarshall(int i, int j, int k) {
if (k < 0) //going through no nodes
return w[i][j];
else
return min(floydwarshall(i, j, k - 1), floydwarshall(i, k, k - 1) + floydwarshall(k, j, k - 1)); //adding the k-th node between i and j
}
void FloydWarshall() {
for (int k = 0; k < n; k++)
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
w[i][j] = min(w[i][j], w[i][k] + w[k][j]); //the route between i and j is the minimum of the root itself and the added roots of the new node added
}
int main() {
fin >> n;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
fin >> w[i][j];
/*for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
for(int k=0;k<n;k++)
w[i][j]= floydwarshall(i,j,k);
floydwarshall(0,0,0);
*/
FloydWarshall();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
fout << w[i][j] << " ";
fout << "\n";
}
return 0;
}