1. 程式人生 > 其它 >Educational DP Contest R - Walk(倍增floyd,矩陣快速冪)

Educational DP Contest R - Walk(倍增floyd,矩陣快速冪)

題目來源:AtCoder EDU DP題集

題目連結:Here

單獨拎出來是因為這道題是一個很好的板子,值得記錄

題意

給定一個 n 個節點的有向圖的鄰接矩陣,求該有向圖中長度為 k 的路徑長。

解法

演算法涉及:倍增 Floyd

答案為該鄰接矩陣的 \(k\) 次冪的行列式。

學過離散數學的後面圖論的話大概都知道求有向圖中長度為 \(k\) 的路徑長的路徑與原始圖的 \(k\) 次方相關,所以只需要求原矩陣的 \(k\) 次冪即可

使用矩陣快速冪即可,時間複雜度 \(\mathcal{O}(n^2log k)\)

const int mod = 1e9 + 7;
ll n, k;
struct Matrix {
    ll mat[50][50];
    void clear() {memset(mat, 0, sizeof(mat));}
    void reset(int n) {
        clear();
        for (int i = 0; i < n; ++i) mat[i][i] = 1;
    }
} a;
Matrix MatrixMul(Matrix a, Matrix b) { // 矩陣快速乘
    Matrix t; t.clear();
    for (int i = 0; i < n; ++i)
        for (int k = 0; k < n; ++k)
            for (int j = 0; j < n; ++j)
                t.mat[i][j] = (t.mat[i][j] + a.mat[i][k] * b.mat[k][j]) % mod;
    return t;
}
ll MatrixQpow(Matrix a, ll p) { // 矩陣快速冪
    Matrix s; s.reset(n);
    for (; p; p >>= 1, a = MatrixMul(a, a))
        if (p & 1) s = MatrixMul(s, a);
    ll sum = 0;
    for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j)
            sum = (sum + s.mat[i][j]) % mod;
    return sum;
}
int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    cin >> n >> k;
    for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) cin >> a.mat[i][j];
    cout << MatrixQpow(a, k);
}

The desire of his soul is the prophecy of his fate
你靈魂的慾望,是你命運的先知。