#include <bits/stdc++.h> using namespace std; const int N = 7000 + 7; const long long inf = 1e18L; int n, k, m; long long dp[N], pv[N]; bool done[N]; vector<pair<int, int>> zelki[N]; inline int mod(int a) { return a >= m ? a - m : a; } int main() { ios::sync_with_stdio(false), cin.tie(nullptr); cin >> n >> k >> m; for (int i = 0; i < n; i++) { int ki, mi, ci; cin >> ki >> mi >> ci; ki--; zelki[ki].emplace_back(mi, ci); } for (int i = 1; i < m; i++) { pv[i] = inf; } for (int i = 0; i < k; i++) { for (int j = 0; j < m; j++) { dp[j] = inf; for (auto&& [mi, ci] : zelki[i]) dp[j] = min(dp[j], pv[mod(j + m - mi)] + ci); } for (int j = 0; j < m; j++) { pv[j] = dp[j]; } } dp[0] = 0LL; for (int rep = 0; rep < m - 1; rep++) { int best = -1; for (int i = 0; i < m; i++) { if (done[i]) continue; if (best == -1 || dp[best] > dp[i]) best = i; } done[best] = true; for (int j = 0; j < m; j++) { int l = mod(best + j); dp[l] = min(dp[l], dp[best] + dp[j]); } } for (int i = 0; i < m; i++) { if (dp[i] == inf) cout << "-1\n"; else cout << dp[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 | #include <bits/stdc++.h> using namespace std; const int N = 7000 + 7; const long long inf = 1e18L; int n, k, m; long long dp[N], pv[N]; bool done[N]; vector<pair<int, int>> zelki[N]; inline int mod(int a) { return a >= m ? a - m : a; } int main() { ios::sync_with_stdio(false), cin.tie(nullptr); cin >> n >> k >> m; for (int i = 0; i < n; i++) { int ki, mi, ci; cin >> ki >> mi >> ci; ki--; zelki[ki].emplace_back(mi, ci); } for (int i = 1; i < m; i++) { pv[i] = inf; } for (int i = 0; i < k; i++) { for (int j = 0; j < m; j++) { dp[j] = inf; for (auto&& [mi, ci] : zelki[i]) dp[j] = min(dp[j], pv[mod(j + m - mi)] + ci); } for (int j = 0; j < m; j++) { pv[j] = dp[j]; } } dp[0] = 0LL; for (int rep = 0; rep < m - 1; rep++) { int best = -1; for (int i = 0; i < m; i++) { if (done[i]) continue; if (best == -1 || dp[best] > dp[i]) best = i; } done[best] = true; for (int j = 0; j < m; j++) { int l = mod(best + j); dp[l] = min(dp[l], dp[best] + dp[j]); } } for (int i = 0; i < m; i++) { if (dp[i] == inf) cout << "-1\n"; else cout << dp[i] << '\n'; } } |