zoukankan      html  css  js  c++  java
  • ZOJ 3497 Mistwald 矩阵

    利用可达矩阵的幂来判断是否可达

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <queue>
    #include <stack>
    #include <map>
    #include <set>
    #include <climits>
    #include <iostream>
    #include <string>
    
    using namespace std;
     
    #define MP make_pair
    #define PB push_back
    typedef long long LL;
    typedef unsigned long long ULL;
    typedef vector<int> VI;
    typedef pair<int, int> PII;
    typedef pair<double, double> PDD;
    const int INF = INT_MAX / 3;
    const double eps = 1e-8;
    const LL LINF = 1e17;
    const LL MOD = LINF;
    const double DINF = 1e60;
    const int maxn = 30;
    
    struct Matrix {
        int n, m;
        LL data[maxn][maxn];
        Matrix(int n = 0, int m = 0): n(n), m(m) {
            memset(data, 0, sizeof(data));
        }
    
        void print() {
            for(int i = 1; i <= n; i++) {
                for(int j = 1; j <= m; j++) {
                    cout << data[i][j] << " ";
                }
                cout << endl;
            }
        }
    };
     
    Matrix operator * (Matrix a, Matrix b) {
        Matrix ret(a.n, b.m);
        for(int i = 1; i <= a.n; i++) {
            for(int j = 1; j <= b.m; j++) {
                for(int k = 1; k <= a.m; k++) {
                    ret.data[i][j] += a.data[i][k] * b.data[k][j];
                    ret.data[i][j] %= MOD;
                }
            }
        }
        return ret;
    }
     
    Matrix operator + (Matrix a, Matrix b) {
        for(int i = 1; i <= a.n; i++) {
            for(int j = 1; j <= a.m; j++) {
                a.data[i][j] += b.data[i][j];
                a.data[i][j] %= MOD;
            }
        }
        return a;
    }
    
    Matrix operator * (int p, Matrix mat) {
        for(int i = 1; i <= mat.n; i++) {
            for(int j = 1; j <= mat.m; i++) {
                mat.data[i][j] *= p;
                mat.data[i][j] %= MOD;
            }
        }
    }
     
    Matrix pow(Matrix mat, LL p) {
        if(p == 0) {
            Matrix ret(mat.n, mat.m);
            for(int i = 1; i <= mat.n; i++) ret.data[i][i] = 1;
            return ret;
        }
        if(p == 1) return mat;
        Matrix ret = pow(mat * mat, p / 2);
        if(p & 1) ret = ret * mat;
        return ret;
    }
    
    int n, m;
    
    int main() {
        int T; scanf("%d", &T);
        while(T--) {
            scanf("%d%d", &n, &m);
            Matrix mat(n * m, n * m);
            for(int i = 1; i <= n; i++) {
                for(int j = 1; j <= m; j++) {
                    int nowx = i - 1, nowy = j - 1, nx, ny;
                    char tmp; scanf(" %c", &tmp);
                    for(int k = 0; k < 4; k++) {
                        if(k) scanf(" %c", &tmp);
                        scanf("(%d,%d)", &nx, &ny);
                        nx--; ny--;
                        if(i == n && j == m) continue;
                        mat.data[nowx * m + nowy + 1][nx * m + ny + 1] = 1;
                    }
                    scanf(" %c", &tmp);
                }
            }
            int Q; scanf("%d", &Q);
            while(Q--) {
                int K; scanf("%d", &K);
                Matrix ret = pow(mat, K);
                if(ret.data[1][n * m] == 0) puts("False");
                else {
                    int cnt = 0;
                    for(int i = 1; i < n * m; i++) {
                        if(ret.data[1][i] != 0) cnt++;
                    }
                    if(cnt == 0) puts("True");
                    else puts("Maybe");
                }
            }
            puts("");
        }
        return 0;
    }
    

      

  • 相关阅读:
    EMQ 解决方案之云平台物联网消息队列解决方案
    EMQ X Broker 3.1 Beta.2 发布
    MQTT 与 Kafka
    EMQ X 助力运营商搭建大规模 NBIoT 平台
    MQTT5.0 消息发布流程
    MQTT 5.0 新特性 |(一) 属性与载荷
    MQTT 5.0 正式成为OASIS标准
    Nginx + Docker 手动集群方式运行 EMQ
    WJQ与机房
    P1505 [国家集训队]旅游
  • 原文地址:https://www.cnblogs.com/rolight/p/4049139.html
Copyright © 2011-2022 走看看