Pagini recente » Cod sursa (job #2788191) | Cod sursa (job #1895560) | Monitorul de evaluare | Cod sursa (job #1890811) | Cod sursa (job #3357156)
#include <iostream>
#include <fstream>
using namespace std;
const int INF = 9999999;
int n;
int C[101][101];
ifstream f("royfloyd.in");
ofstream g("royfloyd.out");
void init()
{
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(i==j) C[i][j]=0;
else C[i][j]=INF;
}
void citire()
{
f >> n;
init();
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
int x;
f >> x;
if(x != 0)
C[i][j] = x;
}
}
void Roy_Floyd()
{
for(int k=1;k<=n;k++)
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(C[i][k] != INF && C[k][j] != INF)
if(C[i][j] > C[i][k] + C[k][j])
C[i][j] = C[i][k] + C[k][j];
}
void afis()
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(C[i][j] == INF)
g << 0 << " ";
else
g << C[i][j] << " ";
}
g << '\n';
}
}
int main()
{
citire();
Roy_Floyd();
afis();
}