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
| #include <iostream> #include <algorithm> #include <cstring> #include <cstdio> using namespace std; const int maxp = 121; const int maxn = 41; typedef long long ll; int n, m, lim; ll k, ans; struct matrix { ll val[maxp][maxp]; matrix() { memset(val, 0, sizeof val); } ll& operator () (int x, int y) { return val[x][y]; } matrix operator * (matrix b) { matrix ret; for (int k = 0; k <= lim; ++k) for (int i = 0; i <= lim; ++i) for (int j = 0; j <= lim; ++j) ret(i, j) += val[i][k] * b(k, j); return ret; } bool check() { ll count = 0; for (int i = 1; i <= n; ++i) { count += (val[i][0] - 1); if (count >= k) return 1; } return 0; } }dou[70], res; int main() { int u, v, w; scanf("%d%d%lld", &n, &m, &k); dou[0](0,0) = 1; lim = 3 * n; for (int i = 1; i <= n; ++i) res(i, i) = dou[0](i, 0) = dou[0](i, i + n) = dou[0](i + n, i + 2 * n) = 1; for (int i = 1; i <= m; ++i) { scanf("%d%d%d", &u, &v, &w); dou[0](u + (w - 1) * n, v)++; } int tim = 1; for(; ; ++tim) { if (tim > 65) { puts("-1"); return 0; } dou[tim] = dou[tim - 1] * dou[tim - 1]; if (dou[tim].check()) break; } tim -= 1; for (; tim >= 0; --tim) { matrix tmp = res * dou[tim]; if (!tmp.check()) res = tmp, ans |= (1ll << tim); } printf("%lld\n", ans); return 0; }
|