传递关系闭包
作者:
Drifter
,
2020-09-23 22:57:40
,
所有人可见
,
阅读 560
沃舍尔算法
//Warshall Algorithm
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 110;
int n;
int M[N][N];
int main(void)
{
cin >> n;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++) cin >> M[i][j];
for(int k = 1; k <= n; k++)
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
if(M[i][j] == 0) M[i][j] = M[i][k] & M[k][j];
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
cout << M[i][j] << ' ';
cout << endl;
}
return 0;
}
应该是
M[i][j] |= M[i][k] & M[k][j];
吧我这个是或运算优化,在布尔积运算中只要有一项是1就行了。