#include using namespace std; #define x first #define y second #define pb push_back #define eb emplace_back #define rep(i,a,b) for(auto i = (a); i != (b); ++i) #define REP(i,n) rep(i,0,n) #define all(v) (v).begin(), (v).end() typedef long long ll; typedef long double ld; typedef pair ii; typedef vector vi; typedef vector vvi; typedef vector vii; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int N; cin >> N; vi indeg(N, 0), outdeg(N, 0); vvi D_AVG(N, vi(N)); REP(i, N) REP(j, N) cin >> D_AVG[i][j]; REP(i, N) D_AVG[i][i] = -1; REP(i, N) REP(j, N) if (D_AVG[i][j] != -1) { indeg[j]++; outdeg[i]++; } queue q; vi T; REP(i, N) if (outdeg[i] == 0) q.push(i); while (!q.empty()) { int cur = q.front(); q.pop(); T.pb(cur); REP(i, N) { if (D_AVG[i][cur] != -1 && --outdeg[i] == 0) q.push(i); } } assert((int) T.size() == N); reverse(all(T)); vi H(N); REP(i, N) H[T[i]] = i; bool sort_property = true; REP(i, N) REP(j, N) { if (D_AVG[i][j] != -1) sort_property &= H[i] < H[j]; } assert(sort_property); vvi npaths(N, vi(N, 0)), sumpaths(N, vi(N, 0)); // REP(i, N) npaths[i][i]++; vii edges; REP(i, N) REP(j, N) if (D_AVG[i][j] != -1) edges.eb(i, j); sort(all(edges), [H] (ii a, ii b) -> bool { int w1 = H[a.y] - H[a.x]; int w2 = H[b.y] - H[b.x]; if (w1 != w2) return w1 < w2; return H[a.y] > H[b.y]; }); vvi RET(N, vi(N, -1)); for (ii pr : edges) { int u = pr.x, w = pr.y; // resolve u -> w REP(v, N) if (RET[u][v]) { npaths[u][w] += npaths[v][w]; sumpaths[u][w] += sumpaths[v][w] + npaths[v][w] * RET[u][v]; } if (npaths[u][w] && D_AVG[u][w] * npaths[u][w] == sumpaths[u][w]) { // no need to add an edge. continue; } ll x = (npaths[u][w] + 1) * D_AVG[u][w] - sumpaths[u][w]; // cerr << u << " -> " << w << ": " << x << endl; // Will we allow for edges with distance 0? assert(x >= 0); RET[u][w] = x; npaths[u][w]++; sumpaths[u][w] += x; assert(sumpaths[u][w] == npaths[u][w] * D_AVG[u][w]); } REP(i, N) REP(j, N) { cout << RET[i][j] << " \n"[j==N-1]; } return 0; }