Cod sursa(job #2944755)

Utilizator DenisONIcBanu Denis Andrei DenisONIc Data 22 noiembrie 2022 22:06:48
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.01 kb
#include <bits/stdc++.h>
using namespace std;
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
#define NMAX 500005
const long double PI = acos(-1);

// Compute the min path in O(n^3)
// Where there is no edge you need to set the value to INF.
void roy_floyd(int n, int **mat) {
  for (int k=1;k<=n;k++) {
    for (int i=1;i<=n;i++) {
      for (int j=1;j<=n;j++) {
        if (i != j) 
          mat[i][j] = min(mat[i][k] + mat[k][j], mat[i][j]);
      }
    }
  }
}

int main(){
  ios::sync_with_stdio(false);
  freopen("royfloyd.in","r",stdin);
  freopen("royfloyd.out","w",stdout);

  int n;
  int **mat;
  cin >> n;
  mat = new int*[n+1];
  for (int i=1;i<=n;i++){
    mat[i] = new int[n+1];
    for (int j=1;j<=n;j++){
      cin >> mat[i][j];
      if (mat[i][j] == 0) {
        mat[i][j] = 1e9;
      }
    }
  }

  roy_floyd(n, mat);

  for (int i=1;i<=n;i++) {
    for (int j=1;j<=n;j++) {
      if (mat[i][j] == (int)1e9) 
        mat[i][j] = 0;
      cout << mat[i][j] << ' ';
    }
    cout << '\n';
  }


  return 0;
}