Pagini recente » Cod sursa (job #3321092) | Cod sursa (job #3354060) | Cod sursa (job #3314693) | Monitorul de evaluare | Cod sursa (job #3348085)
#include <iostream>
#include <fstream>
using namespace std;
int n, A[101][101];
const int inf = 9999999;
ifstream f("royfloyd.in");
ofstream g("royfloyd.out");
void citire()
{
f >> n;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
f >> A[i][j];
if(i!=j && A[i][j] == 0)
A[i][j] = inf;
}
}
}
void rf()
{
int cost;
for(int k = 1; k <= n; k++)
{
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
if(A[i][k] != inf && A[k][j] != inf)
{
cost = A[i][k] + A[k][j];
if(A[i][j] > cost)
A[i][j] = cost;
}
}
}
}
}
void afis()
{
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
if(A[i][j] != inf)
g << A[i][j] << ' ';
else
g << "0 ";
}
g << '\n';
}
}
int main()
{
citire();
rf();
afis();
return 0;
}