Cod sursa(job #2241775)

Utilizator cosmin.pascaruPascaru Cosmin cosmin.pascaru Data 16 septembrie 2018 22:28:13
Problema Floyd-Warshall/Roy-Floyd Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 1.53 kb
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <array>
#include <algorithm>
#include <vector>
#include <assert.h>

using LL = long long;
using ULL = int long long;

const std::string _problemName = "royfloyd";

namespace std {
std::ifstream fin(_problemName + ".in"); 
std::ofstream fout(_problemName + ".out"); 
}

#define cin fin
#define cout fout

int main() {

    int n;
    std::cin >> n;

    std::vector < std::vector<int> > adjMatrix(n, std::vector<int>(n));

    for (int line = 0; line < n; ++line) {
        for (int col = 0; col < n; ++col) {
            std::cin >> adjMatrix[line][col];
        }
    }    

    auto exists = [adjMatrix] (int src, int dst) -> bool {
        return adjMatrix[src][dst] != 0;
    };

    for (int through = 0; through < n; ++through) {
        for (int src = 0; src < n; ++src) {
            if (!exists(src, through)) {
                continue;
            }

            for (int dst = 0; dst < n; ++dst) {
                if (!exists(through, dst) || src == dst) {
                    continue;
                }

                if (!exists(src, dst) || adjMatrix[src][dst] > adjMatrix[src][through] + adjMatrix[through][dst]) {
                    adjMatrix[src][dst] = adjMatrix[src][through] + adjMatrix[through][dst];
                }
            }
        }
    }

    for (int line = 0; line < n; ++line) {
        for (int col = 0; col < n; ++col) {
            std::cout << adjMatrix[line][col] << ' ';
        }
        std::cout << '\n';  
    }    


    return 0;
}