Cod sursa(job #3371216)

Utilizator TheRomulusIvan Remus TheRomulus Data 29 octombrie 2026 11:13:39
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 4.66 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <limits.h>
#include <chrono>
#include <numeric>
#include <iomanip>

#include <vector>
#include <string>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <bitset>
#include <deque>  
#include <stack>
#include <queue>
#include <list>

using namespace std;

// #include <ext/pb_ds/assoc_container.hpp>

// using namespace __gnu_pbds;

// template <class T>
// using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
 

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<string, string> pss;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vii;
typedef vector<pll> vll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<bool> vb;
typedef vector<vb> vvb;

double EPS = 1e-9;
int INF = 1000000005;
long long INFF = 1000000000000000005LL;
double PI = acos(-1);

#define M 1000000007

#define rall(x) (x).rbegin(), (x).rend()
#define all(x) (x).begin(), (x).end()

// auto lim=unique(all(v));
// v.resize(distance(v.begin(), lim));

struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};

struct pair_custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    template <class T1, class T2>
    size_t operator()(const pair<T1, T2>& p) const
    {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();

        auto hash1 = splitmix64(p.first+FIXED_RANDOM);
        auto hash2 = splitmix64(p.second+FIXED_RANDOM);
 
        return hash1^hash2;
    }
};

ll gcd(ll a,ll b){
    ll r;
    while(b){
        r=a%b;
        a=b;
        b=r;
    }
    return a;
}

ll lcm(ll a,ll b){
    return a*b/gcd(a,b);
}

template<class T> T nxt() {
    T x;
    cin >> x;
    return x;
}

template<typename T>
void amin(T &a, T b) {
    a = min(a,b);
}

template<typename T>
void amax(T &a, T b) {
    a = max(a,b);
}

vvl computeDistanceMatrix(const vvl& weights){
    // Takes a matrix representing the weights of edges from 2 nodes.0 means there is no edge between 2 nodes
    int n=weights.size();
    ll maxValue=1e18;
    vvl distances(n,vl(n,0));
    for(int i=0;i<n;++i){
        for(int j=0;j<n;++j){
            ll weight=weights[i][j];
            distances[i][j]=weight?weight:maxValue;
        }
    }

    for(int k=0;k<n;++k){
        for(int i=0;i<n;++i){
            for(int j=0;j<n;++j){
                if(i==j){
                    continue;
                }

                distances[i][j]=min(distances[i][j],distances[i][k]+distances[k][j]);
            }
        }
    }

    for(int i=0;i<n;++i){
        for(int j=0;j<n;++j){
            if(distances[i][j]==maxValue){
                distances[i][j]=0;
            }
        }
    }

    return distances;
}

void Solve(){
    int n;
    cin>>n;
    vvl weights(n,vl(n));
    for(int i=0;i<n;++i){
        generate(all(weights[i]),nxt<ll>);
    }

    vvl distances=computeDistanceMatrix(weights);
    for(int i=0;i<n;++i){
        for(int j=0;j<n;++j){
            cout<<distances[i][j]<<' ';
        }
        cout<<'\n';
    }
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    freopen("royfloyd.in", "r", stdin);
    freopen("royfloyd.out", "w", stdout);

    // std::string file="";
    // std::ifstream in(file+".in");
    // std::streambuf *cinbuf = std::cin.rdbuf(); //save old buf
    // cin.rdbuf(in.rdbuf()); //redirect std::cin
    
    // std::ofstream out(file+".out");
    // std::streambuf *coutbuf = std::cout.rdbuf(); //save old buf
    // cout.rdbuf(out.rdbuf()); //redirect std::cout

    Solve();

    // std::cin.rdbuf(cinbuf);   //reset to standard input again
    // std::cout.rdbuf(coutbuf); //reset to standard output again

    return 0;
}