Cod sursa(job #1967927)

Utilizator mjmilan11Mujdar Milan mjmilan11 Data 17 aprilie 2017 12:28:25
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.92 kb
#include <iostream>
#include <fstream>
#include <algorithm>

using namespace std;

ifstream fin("royfloyd.in");
ofstream fout("royfloyd.out");

int n,i,j,b,dist[101][101],cost;

int main()
{
    fin >> n;
    for(i=1;i<=n;i++)
      for(j=1;j<=n;j++)
        {
            fin >> dist[i][j];
        }

    for(b=1;b<=n;b++)
    {
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=n;j++)
            {
                if(dist[i][b]>0 and dist[b][j]!=0)
                {
                    cost=dist[i][b]+dist[b][j];
                    if((!dist[i][j] || dist[i][j]>cost) and i!=j) dist[i][j]=cost;
                }
                    //dist[i][j]=dist[i][b]+dist[b][j]; /// actualizare distante
            }
        }
    }
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++)
        {
            fout << dist[i][j] << ' ';
        }
        fout << endl;
    }

    return 0;
}