标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2185 Accepted Submission(s):
820
1 #include <iostream> 2 #include <cstring> 3 #include <algorithm> 4 #include <cstdio> 5 using namespace std; 6 const int mod = 1000; 7 struct Mat 8 { 9 int mat[30][30]; 10 }; 11 int n,m; 12 Mat operator * (Mat x, Mat y) 13 { 14 Mat c; 15 memset(c.mat, 0, sizeof(c.mat)); 16 for(int t = 0; t < n; t++) 17 { 18 for(int i = 0; i < n; i++) 19 { 20 for(int j = 0; j < n; j++) 21 { 22 c.mat[i][j] += x.mat[i][t] % mod * (y.mat[t][j] % mod); 23 c.mat[i][j] %= mod; 24 } 25 } 26 } 27 return c; 28 } 29 Mat operator ^ (Mat x,int t) 30 { 31 Mat c; 32 for(int i = 0; i < n; i++) 33 for(int j = 0; j < n; j++) 34 c.mat[i][j] = (i == j); 35 while(t) 36 { 37 if(t & 1) 38 c = c * x; 39 x = x * x; 40 t >>= 1; 41 } 42 return c; 43 } 44 int main() 45 { 46 while(scanf("%d%d", &n, &m) != EOF) 47 { 48 if(n == 0 && m == 0) 49 break; 50 Mat a,temp; 51 int t,A,B,k; 52 memset(a.mat, 0, sizeof(a.mat)); 53 while(m--) 54 { 55 scanf("%d%d", &A,&B); 56 a.mat[A][B] = 1; 57 } 58 scanf("%d", &t); 59 while(t--) 60 { 61 for(int i = 0; i < n; i++) 62 { 63 for(int j = 0; j < n; j++) 64 temp.mat[i][j] = a.mat[i][j]; 65 } 66 scanf("%d%d%d",&A,&B,&k); 67 temp = temp ^ k; 68 printf("%d\n",temp.mat[A][B]); 69 } 70 } 71 return 0; 72 }
HD2157How many wasy??(十大矩阵问题之八 + 邻接矩阵的应用)
标签:
原文地址:http://www.cnblogs.com/zhaopAC/p/5078439.html