-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix_multiplication.cpp
91 lines (67 loc) · 1.57 KB
/
matrix_multiplication.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// BOJ 17401 일하는 세포
#include <bits/stdc++.h>
#define sz size()
#define bk back()
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int MOD = 1e9 + 7;
struct Matrix {
int n;
vector<vector<int>> v;
Matrix(int _n) {
n = _n;
v.resize(n);
for (int i = 0; i < n; i++)
v[i].resize(n);
}
Matrix operator*(const Matrix &o) const {
Matrix ret(n);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
for (int k = 0; k < n; k++)
ret.v[i][j] = (ret.v[i][j] + (ll)v[i][k] * o.v[k][j]) % MOD;
return ret;
}
};
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t, n, d;
cin >> t >> n >> d;
vector<Matrix> graph(t, Matrix(n));
for (int i = 0; i < t; i++) {
int m;
cin >> m;
while (m--) {
int a, b, c;
cin >> a >> b >> c;
graph[i].v[--a][--b] = c;
}
}
int q = d / t;
int r = d % t;
Matrix x(n), y(n), ans(n);
for (int i = 0; i < n; i++)
x.v[i][i] = y.v[i][i] = ans.v[i][i] = 1;
for (int i = 0; i < t; i++) {
x = x * graph[i];
if (i < r)
y = y * graph[i];
}
while (q) {
if (q & 1)
ans = ans * x;
q >>= 1;
x = x * x;
}
ans = ans * y;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
cout << ans.v[i][j] << ' ';
cout << '\n';
}
}