在数学中,在集合 X 上的二元关系 R 的传递闭包是包含 R 的 X 上的最小的传递关系。
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 typedef unsigned long long ull; 5 #define INF 0x3f3f3f3f 6 const ll MAXN = 1e3 + 7; 7 const ll MAXM = 1e4 + 7; 8 const ll MOD = 1e9 + 7; 9 const double pi = acos(-1); 10 int Mat[20][20]; // 11 void Print_Mat(int n) 12 { 13 for (int i = 0; i < n; i++) 14 { 15 for (int j = 0; j < n; j++) 16 cout << Mat[i][j] << " "; 17 cout << endl; 18 } 19 return; 20 } 21 void fun(int n) 22 { 23 for (int k = 0; k < n; k++) 24 for (int i = 0; i < n; i++) 25 for (int j = 0; j < n; j++) 26 if (Mat[i][k] && Mat[k][j]) 27 Mat[i][j] = 1; 28 } 29 int main() 30 { 31 int n; 32 cout << "输入矩阵阶数" << endl; 33 while (cin >> n) 34 { 35 memset(Mat, 0, sizeof(Mat)); 36 cout << "输入矩阵M:" << endl; 37 for (int i = 0; i < n; i++) 38 for (int j = 0; j < n-1; j++) 39 cin >> Mat[i][j]; 40 for (int i = 0; i < n; i++) 41 fun(n); 42 cout << "矩阵M的传递闭包为:" << endl; 43 Print_Mat(n); 44 cout << "输入矩阵阶数" << endl; 45 } 46 return 0; 47 }
原文:https://www.cnblogs.com/graytido/p/10886732.html