bzoj 1875 [SDOI2009]HH去散步 矩阵乘法

题面

题目传送门

解法

如果没有不能经过上一次经过的边这个限制,显然就是矩阵乘法的裸题

那么我们考虑转化一下,把边当成点

将一条无向边拆成2条有向边,然后连边,设邻接矩阵为\(A\)

\(A\)变成\(A^{T-1}\),然后枚举起点的出边,终点的入边即可

时间复杂度:\(O(m^3\ log\ T)\)

代码

#include <bits/stdc++.h>
#define Mod 45989
#define N 150
using namespace std;
template <typename node> void chkmax(node &x, node y) {x = max(x, y);}
template <typename node> void chkmin(node &x, node y) {x = min(x, y);}
template <typename node> void read(node &x) {
    x = 0; int f = 1; char c = getchar();
    while (!isdigit(c)) {if (c == '-') f = -1; c = getchar();}
    while (isdigit(c)) x = x * 10 + c - '0', c = getchar(); x *= f;
}
struct Node {
    int x, y;
} a[N];
struct Matrix {
    int a[N][N];
    void Clear() {memset(a, 0, sizeof(a));}
};
int n, m, T, s, t, tot, cnt;
vector <int> e[N];
Matrix operator * (Matrix x, Matrix y) {
    Matrix ret; ret.Clear();
    for (int k = 2; k <= tot; k++)
        for (int i = 2; i <= tot; i++)
            for (int j = 2; j <= tot; j++)
                ret.a[i][j] = (ret.a[i][j] + x.a[i][k] * y.a[k][j] % Mod) % Mod;
    return ret;
}
Matrix operator ^ (Matrix x, int y) {
    Matrix ret = x; y--;
    while (y) {
        if (y & 1) ret = ret * x;
        y >>= 1, x = x * x;
    }
    return ret;
}
int main() {
    read(n), read(m), read(T), read(s), read(t);
    s++, t++, tot = 1;
    for (int i = 1; i <= m; i++) {
        int x, y; read(x), read(y);
        a[++tot] = (Node) {++x, ++y};
        a[++tot] = (Node) {y, x};
        e[x].push_back(tot - 1), e[x].push_back(tot);
        e[y].push_back(tot - 1), e[y].push_back(tot);
    }
    Matrix tx; tx.Clear();
    for (int i = 1; i <= n; i++)
        for (int j = 0; j < e[i].size(); j++)
            for (int k = 0; k < e[i].size(); k++) {
                int x = e[i][j], y = e[i][k];
                if (x == y || (x ^ 1) == y) continue;
                if (a[x].y == a[y].x) tx.a[x][y] = 1;
            }
    tx = tx ^ (T - 1); int ans = 0;
    for (int i = 2; i <= tot; i++)
        for (int j = 2; j <= tot; j++)
            if (a[i].x == s && a[j].y == t) ans = (ans + tx.a[i][j]) % Mod;
    cout << ans << "\n";
    return 0;
}

转载于:https://www.cnblogs.com/copperoxide/p/9476697.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值