模板

· · 个人记录

矩阵(含乘法与快速幂):

struct matrix{
    int h,w;
    vector<vector<long long>> a;
    matrix(int Int1,int Int2):h(Int1),w(Int2),a(Int1+1,vector<long long>(Int2+1)){}
    matrix operator*(const matrix& x) const{
        matrix b(h,x.w);
        for(int i=1;i<=h;++i){
            for(int j=1;j<=x.w;++j){
                for(int k=1;k<=w;++k){
                    b.a[i][j]+=a[i][k]*x.a[k][j];
                    b.a[i][j]%=MOD;
                }
            }
        }
        return b;
    }
    matrix operator^(const long long& x) const{
        if(x==0){
            matrix b(h,w);
            for(int i=1;i<=h;++i){
                b.a[i][i]=1;
            }
            return b;
        }
        if(x==1) return *this;
        matrix b=*this^(x>>1);
        b=b*b;
        if(x&1) b=b**this;
        return b;
    }
};

AI Dijiskra

    auto dij=[&](int s){
        vector<ll> d(n+1,-1);
        priority_queue<pair<ll,int>,vector<pair<ll,int>>,greater<pair<ll,int>>> q;
        d[s]=0;
        q.push({0,s});
        while(!q.empty()){
            auto [t,x]=q.top();
            q.pop();
            if(t>d[x])continue;
            for(auto [y,w]:g[x]){
                if(d[y]==-1||d[y]>d[x]+w){
                    d[y]=d[x]+w;
                    q.push({d[y],y});
                }
            }
        }
        return d;
    };