传送门:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1898

第一眼看上去就是矩阵乘法,我们发现每过12个时刻,每个鳄鱼的位置都是一定的,所以我们可以每12个一个周期,在周期中暴力乘,在外面用快速幂优化即可。。。

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i ++)
const int maxn = 55;
const int mod = 10000;
int c[maxn][maxn];
struct Matrix {
    int a[maxn][maxn];
    int l, w;
    Matrix() {
        l = w = 0;
        memset(a, 0, sizeof(a));
    }
    Matrix operator * (Matrix t) {
        Matrix tt;
        tt.l = l; tt.w = t.w;
        rep(i, l) rep(j, t.w) rep(k, w) {
            tt.a[i][j] = (tt.a[i][j] + a[i][k] * t.a[k][j]) % mod;
        }
        return tt;
    }
}T[12], ans, res;
int n, m, s, t, k, fn;
int p[5];
void mi(int l) {
    while(l) {
        if(l & 1) res = res * ans;
        ans = ans * ans;
        l >>= 1;
    }
    return;
}
int main() {
    scanf("%d%d%d%d%d", &n, &m, &s, &t, &k);
    int u, v;
    rep(i, m) {
        scanf("%d%d", &u, &v);
        c[u][v] ++; c[v][u] ++;
    }
    rep(i, 12) {
        memcpy(T[i].a, c, sizeof(c));
        T[i].l = T[i].w = n;
    }
    scanf("%d", &fn);
    rep(i, fn) {
        scanf("%d", &p[0]);
        rep(j, p[0]) scanf("%d", &p[j + 1]);
        rep(j, 12) {
            int p1 = p[j % p[0] + 1];
            if(j) {
                rep(x, n) T[j - 1].a[x][p1] = 0;
            }
            rep(x, n) T[j].a[p1][x] = 0;
        }
    }
    ans.l = ans.w = n;
    rep(i, n) ans.a[i][i] = 1;
    res.l = res.w = n;
    rep(i, n) res.a[i][i] = 1;
    rep(i, 12) ans = ans * T[i];
    mi(k / 12);
    rep(i, k % 12) res = res * T[i];
    printf("%d\n", res.a[s][t]);
    return 0;
}