Cod sursa(job #2757064)

Utilizator llalexandruLungu Alexandru Ioan llalexandru Data 3 iunie 2021 19:28:56
Problema Floyd-Warshall/Roy-Floyd Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.16 kb
#include <bits/stdc++.h>
#include <chrono>
using namespace std;
using namespace chrono;
void debug_out() { cerr << endl; }
template<class T> ostream& prnt(ostream& out, T v) { out << v.size() << '\n'; for(auto e : v) out << e << ' '; return out;}
template<class T> ostream& operator<<(ostream& out, vector <T> v) { return prnt(out, v); }
template<class T> ostream& operator<<(ostream& out, set <T> v) { return prnt(out, v); }
template<class T1, class T2> ostream& operator<<(ostream& out, map <T1, T2> v) { return prnt(out, v); }
template<class T1, class T2> ostream& operator<<(ostream& out, pair<T1, T2> p) { return out << '(' << p.first << ' ' << p.second << ')'; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << H; debug_out(T...);}
#define dbg(...) cerr << #__VA_ARGS__ << " ->", debug_out(__VA_ARGS__)
#define dbg_v(x, n) do{cerr<<#x"[]: ";for(int _=0;_<n;++_)cerr<<x[_]<<" ";cerr<<'\n';}while(0)
#define dbg_ok cerr<<"OK!\n"
#define ll long long
#define ld long double
#define ull unsigned long long
#define pii pair<int,int>
#define MOD 1000000007
#define zeros(x) x&(x-1)^x
#define fi first
#define se second

const long double PI = acos(-1);

template<class T>
class MatrixGraph
{
public:
   int n;
   vector< vector<T> > adj;

   MatrixGraph(int n) : n(n) 
   {
      for (int i = 0; i < n; i++) adj.push_back(vector<T>(n));
   }
};

template<class T>
class RFW
{  
public:
   static vector< vector<T> > run(const MatrixGraph<T>& g)
   {
      vector< vector<T> > ans = g.adj;
      for (int k = 0; k < g.n; k++)
         for (int i = 0; i < g.n; i++)
            for (int j = 0; j < g.n; j++)
               ans[i][j] = min(ans[i][j], ans[i][k] + ans[k][j]);
      return ans;
   }
};

int main()
{
   ios::sync_with_stdio(false);

   freopen("royfloyd.in", "r", stdin);
   freopen("royfloyd.out", "w", stdout);
   
   int n;
   cin >> n;
   MatrixGraph<int> g(n);
   for (int i=0; i<n; i++)
   {
      for (int j=0; j<n; j++)
      {
         cin >> g.adj[i][j];
      }
   }
   vector<vector<int>> ans = RFW<int>::run(g);
   for (auto i : ans)
   {
      for (auto j : i)
      {
         cout<<j<<" ";
      }
      cout<< '\n';
   }
   return 0;
}