#include<bits/stdc++.h>
using namespace std;
using LL=long long;
#define FOR(i,l,r)for(int i=(l);i<=(r);++i)
#define REP(i,n)FOR(i,0,(n)-1)
#define ssize(x)int(x.size())
#ifdef DEBUG
auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";}
auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<","+!i++<<e;return o<<"}";}
#define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X)
#else
#define debug(...){}
#endif
int main() {
cin.tie(0)->sync_with_stdio(0);
int n, k, m;
cin >> n >> k >> m;
const LL INF = 1e18;
vector<vector<pair<int, int>>> by_color(k);
REP (i, n) {
int color, mass, cost;
cin >> color >> mass >> cost;
--color;
by_color[color].emplace_back(mass, cost);
}
vector<LL> no_dublicate(m, INF);
no_dublicate[0] = 0;
REP (i, k) {
debug(no_dublicate);
vector<LL> dp(m, INF);
for (auto [mass, cost] : by_color[i]) {
REP (j, m) {
int nj = (j + mass) % m;
dp[nj] = min(dp[nj], no_dublicate[j] + cost);
}
}
no_dublicate = dp;
}
debug(no_dublicate);
vector<LL> dp(m, INF);
dp[0] = 0;
vector<int> odw(m);
REP (xd, m) {
int p = -1;
REP (i, m) {
if (odw[i])
continue;
if (p == -1 || dp[p] > dp[i])
p = i;
}
odw[p] = 1;
REP (i, m) {
int np = p + i;
if (np >= m)
np -= m;
dp[np] = min(dp[np], dp[p] + no_dublicate[i]);
}
}
debug(dp);
REP (i, m) {
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 | #include<bits/stdc++.h> using namespace std; using LL=long long; #define FOR(i,l,r)for(int i=(l);i<=(r);++i) #define REP(i,n)FOR(i,0,(n)-1) #define ssize(x)int(x.size()) #ifdef DEBUG auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";} auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<","+!i++<<e;return o<<"}";} #define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X) #else #define debug(...){} #endif int main() { cin.tie(0)->sync_with_stdio(0); int n, k, m; cin >> n >> k >> m; const LL INF = 1e18; vector<vector<pair<int, int>>> by_color(k); REP (i, n) { int color, mass, cost; cin >> color >> mass >> cost; --color; by_color[color].emplace_back(mass, cost); } vector<LL> no_dublicate(m, INF); no_dublicate[0] = 0; REP (i, k) { debug(no_dublicate); vector<LL> dp(m, INF); for (auto [mass, cost] : by_color[i]) { REP (j, m) { int nj = (j + mass) % m; dp[nj] = min(dp[nj], no_dublicate[j] + cost); } } no_dublicate = dp; } debug(no_dublicate); vector<LL> dp(m, INF); dp[0] = 0; vector<int> odw(m); REP (xd, m) { int p = -1; REP (i, m) { if (odw[i]) continue; if (p == -1 || dp[p] > dp[i]) p = i; } odw[p] = 1; REP (i, m) { int np = p + i; if (np >= m) np -= m; dp[np] = min(dp[np], dp[p] + no_dublicate[i]); } } debug(dp); REP (i, m) { if (dp[i] >= INF) cout << -1 << '\n'; else cout << dp[i] << '\n'; } } |
English