Cod sursa(job #1103947)

Utilizator diana97Diana Ghinea diana97 Data 10 februarie 2014 10:04:17
Problema Floyd-Warshall/Roy-Floyd Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 0.96 kb
#include <iostream>
#include <fstream>
using namespace std;

ifstream f ("royfloyd.in");
ofstream g ("royfloyd.out");


int n;
int cost[101][101];

void citeste () {
    /*f >> n >> m;
    int a, b, c;
    for (int i = 1; i <= m; i++) {
        f >> a >> b >> c;
        cost[a][b] = c;
        cost[b][a] = c;
    }*/
    f>>n;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            f>>cost[i][j];

}

void rezolva () {
    for (int k = 1; k <= n; k++)
        for (int i = 1; i <= n; i++)
            for (int j = 1; j<=n; j++) {
                if (cost[i][k] && cost[k][j])
                cost[i][j] = min (cost[i][j], cost[i][k] + cost[k][j]);
                //cost[j][i] = cost[i][j];
            }
}

void scrieMatrice () {
   for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++)
            g << cost[i][j] << ' ';
        g << '\n';
    }
}

int main () {
    citeste ();
    rezolva ();
    scrieMatrice ();
    return 0;
}