Warshall 算法

求传递闭包

算法步骤略 (直接看代码比较清楚)

#include <bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
inline int lowbit(int x) { return x & (-x); }
#define ll long long
#define pb push_back
#define PII pair<int, int>
#define fi first
#define se second
#define inf 0x3f3f3f3f
const int N = 100;
int a[N][N];

int main() {
    IO;
    int n;
    cin >> n;
    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= n; ++j)
            if (a[j][i])
                for (int k = 1; k <= n; ++k) 
                    a[j][k] = min(1, a[j][k] + a[i][k]);
    
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= n; ++j)
            cout << a[i][j] << " ";
        cout << endl;
    }
    return 0;
}
    
posted @ 2020-12-12 21:36  phr2000  阅读(145)  评论(0编辑  收藏  举报