Cod sursa(job #1689707)

Utilizator razvandRazvan Dumitru razvand Data 14 aprilie 2016 15:10:21
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.28 kb
#include <iostream>
#include <vector>
#include <fstream>
#include <queue>
#include <climits>
#define MAX 103

using namespace std;

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

vector<pair<int, int>> vec[MAX];
int mat[MAX][MAX];
int n,v;

void bell(int src) {

    queue<int> que;
    for(int i = 1; i <= n; i++)
        mat[src][i] = INT_MAX/2-1;
    mat[src][src] = 0;
    que.push(src);
    int nod;
    vector<pair<int, int>>::iterator it;

    while(!que.empty()) {

        nod = que.front();
        que.pop();

        for(it = vec[nod].begin(); it != vec[nod].end(); it++) {

            if(mat[src][nod]+it->second < mat[src][it->first]) {

                mat[src][it->first] = mat[src][nod]+it->second;
                que.push(it->first);

            }

        }

    }

}

int main() {

    in >> n;

    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++) {
            in >> v;
            if(v != 0)
                vec[i].push_back(make_pair(j, v));
        }
    }

    for(int i = 1; i <= n; i++)
        bell(i);

    for(int i = 1; i <= n; i++) {

        for(int j = 1; j <= n; j++) {

            out << mat[i][j] << " ";

        }

        out << '\n';

    }

    return 0;
}