#include <iostream> #include <algorithm> #include <map> #include <set> #include <vector> #define int long long using namespace std; const int M = 7005, inf = 1e18; int n, k, m, cur[M], pre[M], res[M]; vector <pair <int, int>> types[M]; int32_t main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); cin >> n >> k >> m; int a, b, c; for (int i = n; i > 0; --i) { cin >> a >> b >> c; types[a].push_back({ b,c }); } for (int i = m; i >= 0; --i) cur[i] = pre[i] = res[i] = inf; pre[0] = res[0] = 0; for (int i = k; i > 0; --i) { for (auto el : types[i]) for (int j = m - 1; j >= 0; --j) { a = (j + el.first) % m; if (pre[j] + el.second < cur[a]) cur[a] = pre[j] + el.second; } for (int j = m - 1; j >= 0; --j) pre[j] = cur[j], cur[j] = inf; } for (int i = m - 1; i > 0; --i) if (pre[i] < inf) for (int j = m - 1; j >= 0; --j) { a = j * i % m; if (1.0 * pre[i] * j < 1.1 * pre[a]) pre[a] = min(pre[a], pre[i] * j); } for (int i = m - 1; i > 0; --i) if (pre[i] < inf) for (int j = m - 1; j >= 0; --j) { a = (j + i) % m; if (res[j] + pre[i] < res[a]) res[a] = res[j] + pre[i]; } for (int i = 0; i < m; ++i) cout << (res[i] < inf ? res[i] : -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 | #include <iostream> #include <algorithm> #include <map> #include <set> #include <vector> #define int long long using namespace std; const int M = 7005, inf = 1e18; int n, k, m, cur[M], pre[M], res[M]; vector <pair <int, int>> types[M]; int32_t main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); cin >> n >> k >> m; int a, b, c; for (int i = n; i > 0; --i) { cin >> a >> b >> c; types[a].push_back({ b,c }); } for (int i = m; i >= 0; --i) cur[i] = pre[i] = res[i] = inf; pre[0] = res[0] = 0; for (int i = k; i > 0; --i) { for (auto el : types[i]) for (int j = m - 1; j >= 0; --j) { a = (j + el.first) % m; if (pre[j] + el.second < cur[a]) cur[a] = pre[j] + el.second; } for (int j = m - 1; j >= 0; --j) pre[j] = cur[j], cur[j] = inf; } for (int i = m - 1; i > 0; --i) if (pre[i] < inf) for (int j = m - 1; j >= 0; --j) { a = j * i % m; if (1.0 * pre[i] * j < 1.1 * pre[a]) pre[a] = min(pre[a], pre[i] * j); } for (int i = m - 1; i > 0; --i) if (pre[i] < inf) for (int j = m - 1; j >= 0; --j) { a = (j + i) % m; if (res[j] + pre[i] < res[a]) res[a] = res[j] + pre[i]; } for (int i = 0; i < m; ++i) cout << (res[i] < inf ? res[i] : -1) << '\n'; return 0; } |