Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <vector>
- #include <queue>
- using namespace std;
- #define int long long
- int n, d[2500][2500], x[2500][2500];
- const long long INF = 1e15 + 999;
- signed main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(0);
- cout.tie(0);
- cin >> n;
- for (int i = 0; i < n; ++i) {
- for (int j = 0; j < n; ++j) {
- cin >> x[i][j];
- }
- }
- for (int i = 0; i < n; ++i) {
- for (int j = 0; j < n; ++j) {
- if (i == j) {
- d[i][j] = 0;
- }
- else {
- if (x[i][j] != 0) {
- d[i][j] = x[i][j];
- }
- else {
- d[i][j] = INF;
- }
- }
- }
- }
- for (int k = 0; k < n; ++k) {
- for (int i = 0; i < n; ++i) {
- for (int j = 0; j < n; ++j) {
- if (d[i][k] == INF || d[k][j] == INF) continue;
- if (d[i][j] > d[i][k] + d[k][j]) {
- d[i][j] = d[i][k] + d[k][j];
- }
- }
- }
- }
- for (int k = 0; k < n; ++k) {
- for (int i = 0; i < n; ++i) {
- for (int j = 0; j < n; ++j) {
- if (d[k][k] < 0 && d[i][k] != INF && d[k][j] != INF) {
- d[i][j] = -INF;
- }
- }
- }
- }
- for (int i = 0; i < n; ++i) {
- for (int j = 0; j < n; ++j) {
- if (d[i][j] == -INF) {
- cout << 2 << " ";
- }
- else {
- if (d[i][j] < 1e15) {
- cout << 1 << " ";
- }
- else {
- cout << 0 << " ";
- }
- }
- }
- cout << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement