Cod sursa(job #599520)

Utilizator andrianAndrian andrian Data 28 iunie 2011 23:40:04
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.77 kb
#include <iostream>
#include <fstream>
#define nmax 101
#define min(a,b) ((a)<(b))?(a):(b)
using namespace std;

int n;
int a[nmax][nmax];

void citire(){
    ifstream in("royfloyd.in");
    in >> n;
    for(int i=1;i<=n;++i)
        for(int j=1;j<=n;++j)
            in >> a[i][j];
    in.close();
}

void rezolv(){
    int i,j,k;
    for(k=1;k<=n;++k)
        for(i=1;i<=n;++i)
            for(j=1;j<=n;++j)
                if (a[i][k] && a[k][j] && (a[i][j] > a[i][k] + a[k][j] || !a[i][j]) && i != j) a[i][j] = a[i][k] + a[k][j];
}

void afis(){
    ofstream out("royfloyd.out");
    for(int i=1;i<=n;++i){
        for(int j=1;j<=n;++j) out << a[i][j] << " ";
        out << "\n";
    }
    out.close();
}

int main()
{
    citire();
    rezolv();
    afis();

    return 0;
}