Cod sursa(job #2030988)

Utilizator MotoAMotoi Alexandru MotoA Data 2 octombrie 2017 16:31:31
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.66 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("royfloyd.in");
ofstream g("royfloyd.out");
const int inf=10000001;
int c[101][101];
int n,m;
void citire(){
int i,j;
f>>n;
for(i=1;i<=n;i++)
 for(j=1;j<=n;j++){
  f>>c[i][j];
  if(c[i][j]==0 && i!=j)c[i][j]=inf;
 }
}
void RoyFloyd(){
 for(int k=1;k<=n;k++)
  for(int i=1;i<=n;i++)
   for(int j=1;j<=n;j++){
    int cost=c[i][k]+c[k][j];
    if(cost<c[i][j])c[i][j]=cost;
   }
}
void afisare(){
 for(int i=1;i<=n;i++){
  for(int j=1;j<=n;j++)
   if(c[i][j]<inf)g<<c[i][j]<<' ';
    else g<<"0 ";
  g<<'\n';
 }
}
int main(){
 citire();
 RoyFloyd();
 afisare();
}