【矩阵快速幂】经典题 hdu2157 how many ways、woj642 Lost In WHU

How many ways??

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3151    Accepted Submission(s): 1202


Problem Description
春天到了, HDU校园里开满了花, 姹紫嫣红, 非常美丽. 葱头是个爱花的人, 看着校花校草竞相开放, 漫步校园, 心情也变得舒畅. 为了多看看这迷人的校园, 葱头决定, 每次上课都走不同的路线去教室, 但是由于时间问题, 每次只能经过k个地方, 比方说, 这次葱头决定经过2个地方, 那他可以先去问鼎广场看看喷泉, 再去教室, 也可以先到体育场跑几圈, 再到教室. 他非常想知道, 从A 点恰好经过k个点到达B点的方案数, 当然这个数有可能非常大, 所以你只要输出它模上1000的余数就可以了. 你能帮帮他么?? 你可决定了葱头一天能看多少校花哦
 

Input
输入数据有多组, 每组的第一行是2个整数 n, m(0 < n <= 20, m <= 100) 表示校园内共有n个点, 为了方便起见, 点从0到n-1编号,接着有m行, 每行有两个整数 s, t (0<=s,t<n) 表示从s点能到t点, 注意图是有向的.接着的一行是两个整数T,表示有T组询问(1<=T<=100),
接下来的T行, 每行有三个整数 A, B, k, 表示问你从A 点到 B点恰好经过k个点的方案数 (k < 20), 可以走重复边。如果不存在这样的走法, 则输出0
当n, m都为0的时候输入结束
 

Output
计算每次询问的方案数, 由于走法很多, 输出其对1000取模的结果
 

Sample Input
  
  
4 4 0 1 0 2 1 3 2 3 2 0 3 2 0 3 3 3 6 0 1 1 0 0 2 2 0 1 2 2 1 2 1 2 1 0 1 3 0 0
 

Sample Output
  
  
2 0 1 3
 

Author
小黑

经典题目8
给定一个有向图,问从A点恰好走k步(允许重复经过边)到达B点的方案数mod p的值.
把给定的图转为邻接矩阵,即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即可。

代码:
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<map>
using namespace std;
typedef pair<int,int > P;
typedef long long LL;
const int maxn=100;
#define N 25
#define mod 1000

struct Matrix
{
    LL mat[N][N];
    Matrix()
    {
        memset(mat,0,sizeof(mat));
    }
};

Matrix mul(Matrix a,Matrix b)
{
    Matrix res;
    for(int i=0; i<N; i++)
        for(int j=0; j<N; j++)
        {
            for(int k=0; k<N; k++)
            {
                res.mat[i][j]+=a.mat[i][k]*b.mat[k][j];
                res.mat[i][j]%=mod;
            }
        }

    return res;
}

Matrix pow_matrix(Matrix a,LL n)//矩阵快速幂;
{
    Matrix res;
    for(int i=0; i<N; i++) //初始化为单位矩阵;
        res.mat[i][i]=1;
    while(n!=0)
    {
        if(n&1)
            res=mul(res,a);
        a=mul(a,a);
        n>>=1;
    }
    return res;
}

int main()
{
    int n,m;

    while(~scanf("%d%d",&n,&m))
    {
        Matrix A;
        if(n==0&&m==0)
            break;

        int x,y;
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&x,&y);
            A.mat[x][y]=1;
        }

        int T;
        scanf("%d",&T);
        while(T--)
        {
            Matrix B;
            int s,t;
            LL k;
            scanf("%d%d%lld",&s,&t,&k);
            B=pow_matrix(A,k);
            printf("%lld\n",B.mat[s][t]%mod);
        }
    }
    return 0;
}


  642. Lost in WHU
Input file: standard input
Output file: standard output 
Time limit: 1 second
Memory limit: 512 mebibytes

As one of the most beautiful campus in China, Wuhan University is around several hills, so the road is complex and visitors always lose themselves. Give a undirected graph of WHU of NN points and a deadline, then you should count the number of plan to reach the destination by deadline (the starting point is 1 and the ending point is NN).

Input

First line contains the number of points NN (N\le 100N100) and the number of edges MM (M\le N(N-1)/2MN(N1)/2).

The ii-th line of next MM lines contains two numbers u_iui and v_ivi, which respents an edge (u_i, v_i)(ui,vi).

The last line contains the deadline TT(T\le 10^9T109).

Output

The number of plan to reach the destination by deadline module 10^9+7109+7.

Examples

Input 1

4 5
1 3
2 3
3 4
1 2
1 4
8

Output 1

170

这道题要求到达终点后就不能在走了,相比第一题这题的标程进行了一些特别处理;
标程:
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<map>
using namespace std;
typedef pair<int,int > P;
typedef long long LL;
const int maxn=100;
#define N 105
#define mod 1000000007

struct Matrix
{
    LL mat[N][N];
    Matrix()
    {
        memset(mat,0,sizeof(mat));
    }
};

Matrix mul(Matrix a,Matrix b)
{
    Matrix res;
    for(int i=0; i<N; i++)
        for(int j=0; j<N; j++)
        {
            for(int k=0; k<N; k++)
            {
                res.mat[i][j]+=a.mat[i][k]*b.mat[k][j];
                res.mat[i][j]%=mod;
            }
        }

    return res;
}

Matrix pow_matrix(Matrix a,LL n)//矩阵快速幂;
{
    Matrix res;
    for(int i=0; i<N; i++) //初始化为单位矩阵;
        res.mat[i][i]=1;

    while(n!=0)
    {
        if(n&1)
            res=mul(res,a);
        a=mul(a,a);
        n>>=1;
    }
    return res;
}

int main()
{
    int n,m;

    scanf("%d%d",&n,&m);
    Matrix A;
    int x,y;
    for(int i=0; i<m; i++)
    {
        scanf("%d%d",&x,&y);
        if(x!=n)                 //?
        A.mat[x-1][y-1]=1;
        if(y!=n)
        A.mat[y-1][x-1]=1;
    }
    A.mat[n-1][n-1]=1;           //?

    LL T;
    scanf("%lld",&T);
    Matrix B=pow_matrix(A,T);

    printf("%lld\n",B.mat[0][n-1]%mod);
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值