#include <iostream> #include <vector> using namespace std; const long long inf = 1e15; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, k, m; cin >> n >> k >> m; vector<vector<pair<long long, long long>>>T(k + 1); for (int i = 0; i < n; i++) { long long a, b, c; cin >> a >> b >> c; T[a].push_back({ b, c }); } vector<long long>C(m, inf); C[0] = 0; for (int i = 1; i <= k; i++) { vector<long long>K(m, inf); for (auto p : T[i]) { auto M = p.first; auto c = p.second; for (int j = 0; j < m; j++) { K[(j + M) % m] = min(K[(j + M) % m], C[j] + c); } } swap(K, C); } vector<long long>ans(m, inf); for (int M = 0; M < m; M++) { for (long long x = 0; x < m; x++) { ans[(x * M) % m] = min(ans[(x * M) % m], x * C[M]); } } for (int i = 0; i < m; i++) { for (int j = 0; j < m; j++) { ans[(j + i) % m] = min(ans[(j + i) % m], ans[j] + ans[i]); } } for (int i = 0; i < m; i++) if (ans[i] != inf) cout << ans[i] << '\n'; else cout << "-1\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 | #include <iostream> #include <vector> using namespace std; const long long inf = 1e15; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, k, m; cin >> n >> k >> m; vector<vector<pair<long long, long long>>>T(k + 1); for (int i = 0; i < n; i++) { long long a, b, c; cin >> a >> b >> c; T[a].push_back({ b, c }); } vector<long long>C(m, inf); C[0] = 0; for (int i = 1; i <= k; i++) { vector<long long>K(m, inf); for (auto p : T[i]) { auto M = p.first; auto c = p.second; for (int j = 0; j < m; j++) { K[(j + M) % m] = min(K[(j + M) % m], C[j] + c); } } swap(K, C); } vector<long long>ans(m, inf); for (int M = 0; M < m; M++) { for (long long x = 0; x < m; x++) { ans[(x * M) % m] = min(ans[(x * M) % m], x * C[M]); } } for (int i = 0; i < m; i++) { for (int j = 0; j < m; j++) { ans[(j + i) % m] = min(ans[(j + i) % m], ans[j] + ans[i]); } } for (int i = 0; i < m; i++) if (ans[i] != inf) cout << ans[i] << '\n'; else cout << "-1\n"; return 0; } |