#include <bits/stdc++.h> using namespace std; long long n, k, m, mt[7003]; bool bm[7003]; vector < pair < long long, long long > > v[7003]; int main() { scanf("%d%d%d", &n, &k, &m); for (int i = 0; i < n; ++i) { long long a, b, c; scanf("%d%d%d", &a, &b, &c); v[a]. push_back({b, c}); } bm[0] = 1; for (int i = 1; i <= k; ++i) { bool bm2[7003]; long long mt2[7003]; for (int j = 0; j < m; ++j) { bm2[j] = 0; mt2[j] = -1; } mt2[0] = 0; for (auto j : v[i]) { //cout << i << ':'; for (int k = 1; k < m; ++k) { if (bm[(k + m - j.first) % m] && (mt[(k + m - j.first) % m] + j.second < mt2[k] || mt2[k] == -1)) { mt2[k] = mt[(k + m - j.first) % m] + j.second; bm2[k] = 1; } } //for (int j = 0; j < m; ++j) cout << mt2[j] << ' '; //cout << endl; } for (int j = 0; j < m; ++j) { bm[j] = bm2[j]; mt[j] = mt2[j]; } } vector < long long > last; for (int i = 0; i < m; ++i) last.push_back(mt[i]); long long mt2[m]; for (int j = 0; j < m; ++j) mt2[j] = mt[j]; //for (int j = 0; j < m; ++j) cout << mt2[j] << endl; for (int i = 0; i < m; ++i) { if (mt[i] > 0) { for (int j = 2; j <= m; ++j) { if (mt2[(j * i) % m] > mt2[((j - 1) * i )% m] + mt[i] || mt2[(j * i) % m] == -1) { mt2[(j * i) % m] = mt2[((j - 1) * i )% m] + mt[i]; } } } } //for (auto j : mt2) cout << j << ' '; //cout << endl; long long mt3[m]; for (int j = 0; j < m; ++j) mt3[j] = mt2[j]; for (int i = 0; i < m; ++i) { if (mt2[i] > 0) { for (int j = 1; j < m; ++j) { if (mt3[j] > 0 && (mt3[(j + i) % m] == -1 || mt3[(j + i) % m] > mt3[j] + mt2[i])) { mt3[(j + i) % m] = mt3[j] + mt2[i]; } } } } for (auto j : mt3) cout << j << "\n"; return 0; }
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 | #include <bits/stdc++.h> using namespace std; long long n, k, m, mt[7003]; bool bm[7003]; vector < pair < long long, long long > > v[7003]; int main() { scanf("%d%d%d", &n, &k, &m); for (int i = 0; i < n; ++i) { long long a, b, c; scanf("%d%d%d", &a, &b, &c); v[a]. push_back({b, c}); } bm[0] = 1; for (int i = 1; i <= k; ++i) { bool bm2[7003]; long long mt2[7003]; for (int j = 0; j < m; ++j) { bm2[j] = 0; mt2[j] = -1; } mt2[0] = 0; for (auto j : v[i]) { //cout << i << ':'; for (int k = 1; k < m; ++k) { if (bm[(k + m - j.first) % m] && (mt[(k + m - j.first) % m] + j.second < mt2[k] || mt2[k] == -1)) { mt2[k] = mt[(k + m - j.first) % m] + j.second; bm2[k] = 1; } } //for (int j = 0; j < m; ++j) cout << mt2[j] << ' '; //cout << endl; } for (int j = 0; j < m; ++j) { bm[j] = bm2[j]; mt[j] = mt2[j]; } } vector < long long > last; for (int i = 0; i < m; ++i) last.push_back(mt[i]); long long mt2[m]; for (int j = 0; j < m; ++j) mt2[j] = mt[j]; //for (int j = 0; j < m; ++j) cout << mt2[j] << endl; for (int i = 0; i < m; ++i) { if (mt[i] > 0) { for (int j = 2; j <= m; ++j) { if (mt2[(j * i) % m] > mt2[((j - 1) * i )% m] + mt[i] || mt2[(j * i) % m] == -1) { mt2[(j * i) % m] = mt2[((j - 1) * i )% m] + mt[i]; } } } } //for (auto j : mt2) cout << j << ' '; //cout << endl; long long mt3[m]; for (int j = 0; j < m; ++j) mt3[j] = mt2[j]; for (int i = 0; i < m; ++i) { if (mt2[i] > 0) { for (int j = 1; j < m; ++j) { if (mt3[j] > 0 && (mt3[(j + i) % m] == -1 || mt3[(j + i) % m] > mt3[j] + mt2[i])) { mt3[(j + i) % m] = mt3[j] + mt2[i]; } } } } for (auto j : mt3) cout << j << "\n"; return 0; } |