Cod sursa(job #478570)

Utilizator coditzaDiana Kelerman coditza Data 19 august 2010 11:05:57
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.93 kb
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <deque>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <cmath>
 
using namespace std;

template<typename T>
void printV(const vector<T> &v) {
    for (size_t i = 0; i < v.size(); ++i) {
        cout << v[i] << " ";
    }
    cout << endl;
}

class royfloyd {
private:
    typedef vector<int> VI;
    typedef vector<int>::iterator VIIt;

    vector<VI> w;

    void buildGraf(ifstream &in) {
        int n;
        in >> n;

        vector<VI>(n, VI(n, 0)).swap(w);

        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                int c;
                in >> c;

                w[i][j] = c;
            }
        }
    }
public:
    void func_royfloyd(ifstream &in, ofstream &out) {

        buildGraf(in);

        static const int INF = ~(1 << 31);

        vector<VI> p(w);
        for (size_t i = 0; i < p.size(); ++i) {
            for (size_t j = 0; j < p.size(); ++j) {
                if (p[i][j] == 0) {
                    p[i][j] = INF;
                }
            }
            p[i][i] = 0;
        }

        for (size_t k = 0; k < w.size(); ++k) {
            for (size_t i = 0; i < w.size(); ++i) {
                for (size_t j = 0; j < w.size(); ++j) {
                    int np;
                    if (p[i][k] == INF || p[k][j] == INF) {
                        np = INF;
                    } else {
                        np = p[i][k] + p[k][j];
                    }
                    p[i][j] = min(p[i][j], np);
                }
            }
        }

        for (size_t i = 0; i < p.size(); ++i) {
            for (size_t j = 0; j < p.size(); ++j) {
                out << p[i][j] << " ";
            }
            out << endl;
        }
    }
};

int main() {
    ifstream in("royfloyd.in");
    ofstream out("royfloyd.out");

    royfloyd x; x.func_royfloyd(in, out);
}