Cod sursa(job #1713846)

Utilizator TimoteiCopaciu Timotei Timotei Data 6 iunie 2016 19:26:03
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.78 kb
#include <iostream>
#include <fstream>

using namespace std;
int cost[105][105], n;
void read()
{
    ifstream fin("royfloyd.in");
    fin >> n;
    for(int i = 1; i <= n; ++i)
        for(int j = 1; j <= n; ++j)
        fin >> cost[i][j];
}
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(cost[k][j] && cost[i][k] && (cost[i][j] > cost[i][k] + cost[k][j] || !cost[i][j]) && i != j)
        cost[i][j] = cost[i][k] + cost[k][j];
}
void write()
{
    ofstream fout("royfloyd.out");
    for(int i = 1; i <= n; ++i){
        for(int j = 1; j <= n; ++j)
            fout << cost[i][j] << ' ';
        fout << "\n";
    }
}
int main()
{
   read();
   roy_floyd();
   write();
   return 0;
}