[bzoj2208][Jsoi2010]连通数

本文介绍了一种使用bitset优化Floyd算法的方法,通过枚举k并更新i的状态,实现复杂度为$O(\frac{n^{3}

来自FallDream的博客,未经允许,请勿转载,谢谢。


n<=2000

bitset优化floyd , 枚举k,枚举i,如果i能到k,那么i的bitset直接或上k的。复杂度$O(\frac{n^{3}}{32})$

#include<iostream>
#include<cstdio>
#include<bitset>
#define MN 2000
using namespace std;
inline int read()
{
    int x = 0 , f = 1; char ch = getchar();
    while(ch < '0' || ch > '9'){ if(ch == '-') f = -1;  ch = getchar();}
    while(ch >= '0' && ch <= '9'){x = x * 10 + ch - '0';ch = getchar();}
    return x * f;
}

int n,ans=0;
char st[MN+5];
bitset<MN+1> b[MN+2];

int main()
{
    n=read();
    for(int i=1;i<=n;i++)
    {
        scanf("%s",st+1);
        for(int j=1;j<=n;j++)
            b[i][j]=(st[j]=='1'||i==j)?1:0;    
    }
    for(int k=1;k<=n;k++)
        for(int i=1;i<=n;i++)
            if(b[i][k]) b[i]|=b[k];
    for(int i=1;i<=n;i++) ans+=b[i].count();
    cout<<ans;
    return 0;
}

 

转载于:https://www.cnblogs.com/FallDream/p/bzoj2208.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值