Pagini recente » Cod sursa (job #596249) | Cod sursa (job #2470530) | Cod sursa (job #2706021) | Cod sursa (job #471828)
Cod sursa(job #471828)
#include <fstream>
using namespace std;
const char InFile[]="royfloyd.in";
const char OutFile[]="royfloyd.out";
const int MaxN=105;
const int INF=1<<30;
ifstream fin(InFile);
ofstream fout(OutFile);
int N,path_cost[MaxN][MaxN],edge_cost[MaxN][MaxN];
inline int int_min(int a, int b)
{
if(a<b)return a;
return b;
}
inline void royfloyd()
{
for(register int i=1;i<=N;++i)
{
for(register int j=1;j<=N;++j)
{
path_cost[i][j]=edge_cost[i][j];
}
}
for(register int k=1;k<=N;++k)
{
for(register int i=1;i<=N;++i)
{
for(register int j=1;j<=N;++j)
{
path_cost[i][j]=int_min(path_cost[i][j],path_cost[i][k]+path_cost[k][j]);
}
}
}
}
int main()
{
fin>>N;
for(register int i=1;i<=N;++i)
{
for(register int j=1;j<=N;++j)
{
fin>>edge_cost[i][j];
if(!edge_cost[i][j])
{
edge_cost[i][j]=INF;
}
}
edge_cost[i][i]=0;
}
fin.close();
royfloyd();
for(register int i=1;i<=N;++i)
{
for(register int j=1;j<=N;++j)
{
fout<<path_cost[i][j]<<" ";
}
fout<<"\n";
}
fout.close();
return 0;
}