#include <bits/stdc++.h> using namespace std; constexpr int maxK = 7e3, maxM = 7e3; constexpr long long INF = 4e18; int n, k, m; int k_, m_, c; vector<pair<int, int>> zelki[maxK + 1]; vector<long long> getWeights() { vector<long long> res(m, INF); for(pair<int, int> &i : zelki[1]) { res[(i.first == m ? 0 : i.first)] = min( res[(i.first == m ? 0 : i.first)], (long long)i.second ); } for(int i=2; i<=k; i++) { vector<long long> new_res(m, INF); for(pair<int, int> &j : zelki[i]) { for(int ii=0; ii<=m-j.first-1; ii++) { new_res[j.first + ii] = min( new_res[j.first + ii], res[ii] + j.second ); } for(int ii=m-j.first; ii<=m-1; ii++) { new_res[j.first + ii - m] = min( new_res[j.first + ii - m], res[ii] + j.second ); } } res = new_res; } res[0] = 0; return res; } vector<long long> updateWeights(vector<long long> &weights) { vector<long long> res = weights; for(int i=0; i<=m-1; i++) { for(int j=0; j<=m-i-1; j++) { res[i + j] = min( res[i + j], weights[i] + weights[j] ); } for(int j=m-i; j<=m-1; j++) { res[i + j - m] = min( res[i + j - m], weights[i] + weights[j] ); } } return res; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> k >> m; for(int i=1; i<=n; i++) { cin >> k_ >> m_ >> c; zelki[k_].push_back({m_, c}); } vector<long long> weights = getWeights(); for(int i=1; i<=13; i++) { weights = updateWeights(weights); } for(int i=0; i<=m-1; i++) { cout << (weights[i] == INF ? -1 : weights[i]) << '\n'; } }
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 92 | #include <bits/stdc++.h> using namespace std; constexpr int maxK = 7e3, maxM = 7e3; constexpr long long INF = 4e18; int n, k, m; int k_, m_, c; vector<pair<int, int>> zelki[maxK + 1]; vector<long long> getWeights() { vector<long long> res(m, INF); for(pair<int, int> &i : zelki[1]) { res[(i.first == m ? 0 : i.first)] = min( res[(i.first == m ? 0 : i.first)], (long long)i.second ); } for(int i=2; i<=k; i++) { vector<long long> new_res(m, INF); for(pair<int, int> &j : zelki[i]) { for(int ii=0; ii<=m-j.first-1; ii++) { new_res[j.first + ii] = min( new_res[j.first + ii], res[ii] + j.second ); } for(int ii=m-j.first; ii<=m-1; ii++) { new_res[j.first + ii - m] = min( new_res[j.first + ii - m], res[ii] + j.second ); } } res = new_res; } res[0] = 0; return res; } vector<long long> updateWeights(vector<long long> &weights) { vector<long long> res = weights; for(int i=0; i<=m-1; i++) { for(int j=0; j<=m-i-1; j++) { res[i + j] = min( res[i + j], weights[i] + weights[j] ); } for(int j=m-i; j<=m-1; j++) { res[i + j - m] = min( res[i + j - m], weights[i] + weights[j] ); } } return res; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> k >> m; for(int i=1; i<=n; i++) { cin >> k_ >> m_ >> c; zelki[k_].push_back({m_, c}); } vector<long long> weights = getWeights(); for(int i=1; i<=13; i++) { weights = updateWeights(weights); } for(int i=0; i<=m-1; i++) { cout << (weights[i] == INF ? -1 : weights[i]) << '\n'; } } |