#include <bits/stdc++.h>
using namespace std;
#define rep(a, b) for (int a = 0; a < (b); a++)
#define rep1(a, b) for (int a = 1; a <= (b); a++)
#define all(x) (x).begin(), (x).end()
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int MOD = 1e9 + 7;
#define LOCAL false
const ll INF = 1e18 + 7;
const int LIM = 7007;
int n, colorcnt, R;
vector<pii> vals[LIM];
ll dp[2][LIM];
bool changed[2][LIM];
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
if (LOCAL) {
ignore=freopen("io/in", "r", stdin);
ignore=freopen("io/out", "w", stdout);
}
cin >> n >> colorcnt >> R;
int k, m, c;
rep(i, n) {
cin >> k >> m >> c;
vals[k].push_back({m, c});
}
rep(r, R) dp[0][r] = INF;
dp[0][0] = 0;
int cur = 0;
rep1(k, colorcnt) {
rep(r, R) dp[cur^1][r] = INF;
for (auto [m, c]: vals[k]) {
rep(r, R) {
int newr = (r+m)%R;
dp[cur^1][newr] = min(dp[cur^1][newr], c+dp[cur][r]);
}
}
cur ^= 1;
}
dp[cur][0] = 0;
rep(r, R) changed[cur][r] = true;
bool change;
while (true) {
change = false;
rep(r, R) {
dp[cur^1][r] = dp[cur][r];
changed[cur^1][r] = false;
}
rep(i, R) if (changed[cur][i]) rep(j, R) {
int idx = (i+j)%R;
ll newval = dp[cur][i]+dp[cur][j];
if (newval < dp[cur^1][idx]) {
dp[cur^1][idx] = newval;
change = true;
changed[cur^1][idx] = true;
}
}
cur ^= 1;
if (!change) break;
}
rep(r, R) cout << (dp[cur][r] != INF ? dp[cur][r] : -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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | #include <bits/stdc++.h> using namespace std; #define rep(a, b) for (int a = 0; a < (b); a++) #define rep1(a, b) for (int a = 1; a <= (b); a++) #define all(x) (x).begin(), (x).end() using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; const int MOD = 1e9 + 7; #define LOCAL false const ll INF = 1e18 + 7; const int LIM = 7007; int n, colorcnt, R; vector<pii> vals[LIM]; ll dp[2][LIM]; bool changed[2][LIM]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); if (LOCAL) { ignore=freopen("io/in", "r", stdin); ignore=freopen("io/out", "w", stdout); } cin >> n >> colorcnt >> R; int k, m, c; rep(i, n) { cin >> k >> m >> c; vals[k].push_back({m, c}); } rep(r, R) dp[0][r] = INF; dp[0][0] = 0; int cur = 0; rep1(k, colorcnt) { rep(r, R) dp[cur^1][r] = INF; for (auto [m, c]: vals[k]) { rep(r, R) { int newr = (r+m)%R; dp[cur^1][newr] = min(dp[cur^1][newr], c+dp[cur][r]); } } cur ^= 1; } dp[cur][0] = 0; rep(r, R) changed[cur][r] = true; bool change; while (true) { change = false; rep(r, R) { dp[cur^1][r] = dp[cur][r]; changed[cur^1][r] = false; } rep(i, R) if (changed[cur][i]) rep(j, R) { int idx = (i+j)%R; ll newval = dp[cur][i]+dp[cur][j]; if (newval < dp[cur^1][idx]) { dp[cur^1][idx] = newval; change = true; changed[cur^1][idx] = true; } } cur ^= 1; if (!change) break; } rep(r, R) cout << (dp[cur][r] != INF ? dp[cur][r] : -1) << '\n'; return 0; } |
English