[SCOI2009]迷路

要是这个图的权是1的话就和Cow Relay 一样了。

因为n非常的小,我们把它扩大9倍,每个节点拆成九个,代表距离为1,2,3,4,5,6,7,8,9。

然后连边的时候就连i+len->j就行了。(好想捏radish)

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4 #include <cstring>
 5 using namespace std;
 6 int n,T;
 7 const int mod=2009;
 8 struct Mat{
 9     int g[91][91];
10     Mat(){memset(g,0,sizeof g);}
11     Mat operator *(const Mat &a) const{
12         Mat ans;
13         for(int i=1;i<=n*9;i++) {
14             for(int j=1;j<=n*9;j++) {
15 //                if(g[i][k])
16                 for(int k=1;k<=n*9;k++) {
17                     ans.g[i][j]=(ans.g[i][j]+g[i][k]*a.g[k][j])%mod;
18                 }
19             }
20         }
21         return ans;
22     }
23 }A,B;
24 char s[11][11];
25 Mat ksm(Mat x,int y){
26     Mat ans=x;y--;
27     while(y) {
28         if(y&1) ans=ans*x;
29         x=x*x;y>>=1;
30     }
31     return ans;
32 }
33 int main() {
34     scanf("%d%d",&n,&T);
35     for(int i=1;i<=n;i++) {
36         for(int j=1;j<=8;j++) {
37             A.g[(i-1)*9+j][(i-1)*9+j+1]=1;
38         }
39     }
40     for(int i=1;i<=n;i++) {
41         scanf("%s",s[i]);
42         for(int j=1;j<=n;j++) {
43             int tp=s[i][j-1]-'0';if(tp)A.g[(i-1)*9+tp][(j-1)*9+1]=1;
44         }
45     }
46     B=ksm(A,T);
47     cout<<B.g[1][(n-1)*9+1];
48 }
unit lost

猜你喜欢

转载自www.cnblogs.com/sdfzhsz/p/9647892.html