Pagini recente » Arhiva de probleme | Cod sursa (job #461750) | Cod sursa (job #804996) | Cod sursa (job #408026) | Cod sursa (job #2757068)
#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;
T inexistent;
vector< vector<T> > adj;
MatrixGraph(int n, T inexistent = -1) : n(n), inexistent(inexistent)
{
for (int i = 0; i < n; i++)
{
vector<T> v(n);
fill(v.begin(), v.end(), inexistent);
adj.push_back(v);
}
}
};
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++)
if (ans[i][k] != g.inexistent && ans[k][j] != g.inexistent && ans[i][j] > ans[i][k] + ans[k][j])
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, 0);
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;
}