fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define int long long
  4. const int mod=1e9+7,N=205;
  5. struct matrix
  6. {
  7. int n,m;
  8. int A[N][N];
  9. matrix(int _n,int _m)
  10. {
  11. n=_n;
  12. m=_m;
  13. memset(A,0,sizeof(A));
  14. }
  15. matrix operator*(const matrix &e) const
  16. {
  17. matrix f(n,e.m);
  18. for(int a=0;a<f.n;a++){
  19. for(int b=0;b<f.m;b++){
  20. for(int c=0;c<m;c++){
  21. f.A[a][b]+=A[a][c]*e.A[c][b];
  22. f.A[a][b]%=mod;
  23. }
  24. }
  25. }
  26. return f;
  27. }
  28. matrix operator^(int k) const
  29. {
  30. matrix res(n,n);
  31. for(int a=0;a<n;a++){
  32. res.A[a][a]=1;
  33. }
  34. matrix mul=*this;
  35. while(k>0){
  36. if(k&1) res=res*mul;
  37. mul=mul*mul;
  38. k/=2;
  39. }
  40. return res;
  41. }
  42. };
  43. signed main() {
  44. ios::sync_with_stdio(0);
  45. cin.tie(0);
  46.  
  47. int n, m, q;
  48. cin >> n >> m >> q;
  49.  
  50. matrix A(n,n);
  51.  
  52. for (int i = 0; i < m; i++) {
  53. int u, v; cin >> u >> v;
  54. u--, v--;
  55. A.A[u][v] = 1; // Directed edge
  56. }
  57.  
  58.  
  59. while (q--) {
  60. int s, t, k;
  61. cin >> s >> t >> k;
  62. s--, t--;
  63. A=A^k;
  64.  
  65. cout << A.A[s][t] << '\n';
  66. }
  67. }
  68.  
Success #stdin #stdout 0.01s 5316KB
stdin
3 4 4
1 2
2 3
3 1
2 1
1 2 6
3 3 5
1 3 1
3 2 54
stdout
2
816
1081
498588477