洛谷 B3611 【模板】传递闭包
只要距离不是0x3f3f3 就代表能联通
#include <bits/stdc++.h>
using namespace std;//防X抄袭
int n;
int a[110][110];
#define inf 0x3f3f3f3f
int main()
{
cin >> n;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
{
cin >> a[i][j];
if(a[i][j]==0)
a[i][j]= inf;
}
for (int k = 1; k <= n; k++)//课上讲的floyd
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if(a[i][j]>a[i][k]+a[k][j] )
a[i][j]=a[i][k]+a[k][j];
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
if(a[i][j]==inf)
cout<<"0 ";
else
cout <<"1 ";
cout << endl;
}
return 0;
}