一直不明白为什么要用floyd求传递闭包,直接搜不是更快嘛……不过其实可以用bitset优化,方法也比较显然。bitset是真的神奇啊,好多01状态且转移相似的东西都可以用这个优化一下。
#include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> #include<cstring> #include<algorithm> #include<bitset> using namespace std; int read() { int x=0,f=1;char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();} while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar(); return x*f; } #define N 2010 int n,ans=0; bitset<N> a[N]; int main() { freopen("bzoj2208.in","r",stdin); freopen("bzoj2208.out","w",stdout); n=read(); for (int i=1;i<=n;i++) { char c=getchar(); while (c!='0'&&c!='1') c=getchar(); for (int j=1;j<=n;j++) a[i][j]=c^48,c=getchar(); a[i][i]=1; } for (int k=1;k<=n;k++) for (int i=1;i<=n;i++) if (a[i][k]) a[i]|=a[k]; for (int i=1;i<=n;i++) ans+=a[i].count(); cout<<ans; fclose(stdin);fclose(stdout); return 0; }