【离散数学】Warshall算法实现 传递闭包对应矩阵
测试样例,数据拿离散书上Page 124页测的:
/*
7
1 1 0 0 0 0 0
0 0 0 1 0 0 0
0 0 0 0 1 0 0
0 1 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
*/
代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <vector> using namespace std; const int len = 1000 + 100; int M[len][len]; //原方阵 int A[len][len]; //置新后方阵 int n, i, j; //n表示方阵行数 void Wareshall() { for(i = 1; i <= n; i++) { for(j = 1; j <= n; j++) { if(A[j][i] == 1) { for(int k = 1; k <= n; k++) { A[j][k] = A[j][k] + A[i][k]; if(A[j][k] >= 1) { A[j][k] = 1; } } } } } } int main() { freopen("datain.txt","r",stdin); cout << "输入原方阵行数:" << endl; cin >> n; cout << endl; cout << "输入原关系矩阵(方阵):"; for(i = 1; i <= n; i++) { for(j = 1; j <= n; j++) { cin >> M[i][j]; A[i][j] = M[i][j]; } } Wareshall(); cout << "输出传递闭包对应关系矩阵:" << endl; for(i = 1; i <= n; i++) { for(j = 1; j <= n; j++) { cout << A[i][j] <<" "; } cout << endl; } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
posted on 2015-10-01 02:54 Tob__yuhong 阅读(1137) 评论(0) 编辑 收藏 举报