zoukankan      html  css  js  c++  java
  • zoj3497(经典矩阵乘法)

    原以为是用搜索做的题,想了好久都无法想到一个高效正确的解法。 

    后面发现竟然这就是矩阵的应用! 碉堡! 

    给定一个有向图,问从A点恰好走k步(允许重复经过边)到达B点的方案数mod p的值  ——选自matrix67
        把给定的图转为邻接矩阵,即A(i,j)=1当且仅当存在一条边i->j。令C=A*A,那么C(i,j)=ΣA(i,k)*A(k,j),实际上就等于从点i到点j恰好经过2条边的路径数(枚举k为中转点)。类似地,C*A的第i行第j列就表示从i到j经过3条边的路径数。同理,如果要求经过k步的路径数,我们只需要二分求出A^k即可。

    K - Mistwald
    Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

    Description

    In chapter 4 of the game Trails in the Sky SC, Estelle Bright and her friends are crossing Mistwald to meet their final enemy, Lucciola.

    Mistwald is a mysterious place. It consists of M * N scenes, named Scene (1, 1) to Scene (MN). Estelle Bright and her friends are initially at Scene (1, 1), the entering scene. They should leave Mistwald from Scene (MN), the exiting scene. Note that once they reach the exiting scene, they leave Mistwald and cannot come back. A scene in Mistwald has four exits, north, west, south, and east ones. These exits are controlled by Lucciola. They may not lead to adjacent scenes. However, an exit can and must lead to one scene in Mistwald.

    Estelle Bright and her friends walk very fast. It only takes them 1 second to cross an exit, leaving a scene and entering a new scene. Other time such as staying and resting can be ignored. It is obvious that the quicker they leave Mistwald, the better.

    Now you are competing with your roommate for who uses less time to leave Mistwald. Your roommate says that he only uses P seconds. It is known that he lies from time to time. Thus, you may want to code and find out whether it is a lie.

    Input

    There are multiple test cases. The first line of input is an integer T ≈ 10 indicating the number of test cases.

    Each test case begins with a line of two integers M and N (1 ≤ MN ≤ 5), separated by a single space, indicating the size of Mistwald. In the next M lines, the ith line contains N pieces of scene information, separated by spaces, describing Scene (i, 1) to Scene (iN). A scene description has the form "((x1,y1),(x2,y2),(x3,y3),(x4,y4))" (1 ≤ xk ≤ M; 1 ≤ yk ≤ N; 1 ≤ k ≤ 4) indicating the locations of new scenes the four exits lead to. The following line contains an integer Q (1 ≤ Q ≤ 100). In the next Q lines, each line contains an integer P (0 ≤ P ≤ 100,000,000), which is the time your roommate tells you.

    Test cases are separated by a blank line.

    Output

    For each P, output one of the following strings in one line: "True" if it cannot be a lie; "Maybe" if it can be a lie; "False" if it must be a lie.

    Print a blank line after each case.

    Sample Input

    2
    3 2
    ((3,1),(3,2),(1,2),(2,1)) ((3,1),(3,1),(3,1),(3,1))
    ((2,1),(2,1),(2,1),(2,2)) ((3,2),(3,2),(3,2),(3,2))
    ((3,1),(3,1),(3,1),(3,1)) ((3,2),(3,2),(3,2),(1,1))
    3
    1
    2
    10
    
    2 1
    ((2,1),(2,1),(2,1),(2,1))
    ((2,1),(2,1),(2,1),(2,1))
    2
    1
    2
    

    Sample Output

    Maybe
    False
    Maybe
    
    True
    False
    
    
     
     
    #include <iostream>
    #include <string.h>
    #include <stdio.h>
    #include <algorithm>
    using namespace std;
    
    int n,m;
    bool g[33][33];
    int x[10],y[10];
    bool tg[33][33];
    
    void mul(bool s[33][33],bool t[33][33])
    {
        bool tmp[33][33];
        int top=n*m;
        memset(tmp,0,sizeof(tmp));
        for(int k=0;k<top;k++)
            for(int i=0;i<top;i++)
                for(int j=0;j<top;j++)
                {
                    tmp[i][j]|=(s[i][k]&t[k][j]);
                }
    
        for(int i=0;i<top;i++)
            for(int j=0;j<top;j++)
                s[i][j]=tmp[i][j];
    
    }
    
    int main()
    {
        int T;
        scanf("%d",&T);
        while(T--)
        {
            memset(g,0,sizeof(g));
    
            scanf("%d%d",&n,&m);
            for(int i=0;i<n;i++)
                for(int j=0;j<m;j++)
                {
                    int id=i*m+j;
                    scanf(" ((%d,%d),(%d,%d),(%d,%d),(%d,%d))",&x[1],&y[1],&x[2],&y[2],&x[3],&y[3],&x[4],&y[4]);
                    if(i!=n-1||j!=m-1)
                    {
                        for(int k=1;k<=4;k++)
                        {
                            int tid=(x[k]-1)*m+y[k]-1;
                            g[id][tid]=1;
                        }
                    }
                }
            int q;
            scanf("%d",&q);
            while(q--)
            {
                int tmp;
                scanf("%d",&tmp);
                bool sum[33][33];
                for(int i=0;i<n*m;i++)
                    for(int j=0;j<n*m;j++)
                    {
                        if(i==j) sum[i][j]=1;
                        else sum[i][j]=0;
                        tg[i][j]=g[i][j];
                    }
                while(tmp)
                {
                    if((tmp&1)) mul(sum,tg);
                    mul(tg,tg);
                    tmp>>=1;
                }
                if(sum[0][n*m-1]==0) printf("False
    ");
                else
                {
                    int flag=0;
                    for(int i=1;i<n*m-1;i++)
                    {
                        if(g[0][i]!=0)
                        {
                            flag=1;
                            break;
                        }
                    }
                    if(flag) printf("Maybe
    ");
                    else printf("True
    ");
                }
            }
            printf("
    ");
        }
        return 0;
    }
  • 相关阅读:
    杂想
    杂题操作
    codeforces 11D(状压dp)
    2019 计蒜之道 复赛 “星云系统” (单调栈)
    SPOJ VLATTICE (莫比乌斯反演)
    2019 ICPC 陕西西安邀请赛 D. Miku and Generals
    buerdepepeqi 的模版
    HDU 2588 GCD
    二项式反演
    2014ACM/ICPC亚洲区西安站 F题 color (组合数学,容斥原理)
  • 原文地址:https://www.cnblogs.com/chenhuan001/p/3422245.html
Copyright © 2011-2022 走看看