路线统计(codevs 1482)

题目描述 Description

N个节点的有向图, 求从start到finish刚好经过时间time的总方案数 mod 502630.

输入描述 Input Description

第一行包含一个整数n, 所有点是从0到N-1编号.

       接下来n行,每行包含n个字符. 第i行第j个字符表示i到j需要的时间. 字符只可能是’1’到’5’, 或者是’.’表示i不能到达j. 保证主对角线都是’.’.

       接下来一行3个整数start, finish, time.

输出描述 Output Description

输出总方案数.

样例输入 Sample Input

       3

       .12

       2.1

       12.

       0 2 5

样例输出 Sample Output

8

数据范围及提示 Data Size & Hint

       对于20%的数据, 输入的字符不是’1’就是’.’;

       对于100%的数据, 1 <= n <= 10; 1 <= start,finish <= n; 1 <= time <= 10^9.

/*
  矩阵乘法快速幂
  本题路径长度较小,所以将路径长度为n的边拓展成n条路径长度为1的边来做
*/
#include<cstdio>
#include<iostream>
#include<cstring>
#define M 60
#define mod 502630
using namespace std;
struct node
{
    int map[M][M];
    node()
    {
        memset(map,0,sizeof(map));
    }
};node yong;
int n,s,t,step;
node jv(node a,node b)
{
    node c;
    for(int k=1;k<=n*5;k++)
      for(int i=1;i<=n*5;i++)
        for(int j=1;j<=n*5;j++)
          c.map[i][j]+=((long long)a.map[i][k]*b.map[k][j]%mod)%mod,
          c.map[i][j]%=mod;
    return c;
}
void work()
{
    node ans;
    for(int i=1;i<=5*n;i++)
      ans.map[i][i]=1;
    while(step)
    {
        if(step&1)ans=jv(ans,yong);
        yong=jv(yong,yong);
        step/=2;
    }
        printf("%d",ans.map[s+1][t+1]);
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
      for(int j=1;j<=n;j++)
      {
          char c;
          cin>>c;
          if(c!='.')
          {
              int x=c-'0';
              for(int k=1;k<x;k++)
                yong.map[(k-1)*n+i][k*n+i]=1;
              yong.map[(x-1)*n+i][j]=1;
          }
      }
    scanf("%d%d%d",&s,&t,&step);
    work();
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/harden/p/5737971.html