Pagini recente » Cod sursa (job #2462372) | Cod sursa (job #664674) | Cod sursa (job #1443750) | Cod sursa (job #2859258)
#include "bits/stdc++.h"
using namespace std;
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
using ll = long long;
using ull = unsigned long long;
const int mxN = 100;
const int INF = 1e9;
int W[mxN][mxN], dist[mxN][mxN];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
freopen("royfloyd.in", "r", stdin);
freopen("royfloyd.out", "w", stdout);
int n;
cin >> n;
// reading
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cin >> W[i][j];
}
}
// initialization
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (i == j) {
dist[i][j] = 0;
} else if (W[i][j]) {
dist[i][j] = W[i][j];
} else {
dist[i][j] = INF;
}
}
}
// Floyd-Warshall
for (int k = 0; k < n; ++k) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
}
}
}
// Answer
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cout << dist[i][j] << ' ';
}
cout << "\n";
}
return 0;
}