Cod sursa(job #1849171)

Utilizator jason2013Andronache Riccardo jason2013 Data 17 ianuarie 2017 09:10:56
Problema Floyd-Warshall/Roy-Floyd Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.19 kb
#include <bits/stdc++.h>
#define PINF 1000
using namespace std;
ofstream g("royfloyd.out");

const int NMAX = 100;
int n, m;
int mat[NMAX][NMAX];

void constr_mat();
void afisare_mat();
void roy_floyd();

int main()
{
    constr_mat();
    //afisare_mat();
    roy_floyd();
    afisare_mat();
    return 0;
}

void constr_mat()
{
    //int i, j, x, y, k, c;
    ifstream f("royfloyd.in");
    f>>n;
    /*

    f>>n>>m;
    for(i = 1; i <= n; i++)
        for(j = 1; j <= n; j++)
            if(i == j) mat[i][j] = 0;
            else mat[i][j] = PINF;

    for(k = 1; k <= m; k++)
    {
        f>>x>>y>>c;
        mat[x][y] = mat[y][x] = c;
    }
    */

    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
            f>>mat[i][j];
    f.close();
}

void afisare_mat()
{
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++)
            g<<mat[i][j]<<" ";
        g<<"\n";
    }
}

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((mat[i][j] > mat[i][k] + mat[k][j]) && i != k && j != k) mat[i][j] = mat[i][k] + mat[k][j];
}