#include <bits/stdc++.h> using namespace std; #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 #define ll long long #define all(x) x.begin(), x.end() #define pb push_back #define fi first #define se second typedef pair <int, int> pii; constexpr ll INF = 1e18+7; constexpr ll N = 14e3+7; int popr[N]; int npopr[N]; bool kol[N]; void test() { int n, k, m; cin>>n>>k>>m; vector <vector <pii> > Z(k, vector <pii>()); for (int i=0; i<n; ++i) { int a, b, c; cin>>a>>b>>c; --a; b %= m; Z[a].pb({b, c}); } vector <ll> comp(m*2, INF); vector <ll> prev(m*2, INF); prev[0] = 0; for (int col=0; col<k; ++col) { for (auto &[mass, cost] : Z[col]) { for (int j=m-1; j>=0; --j) { int nm = mass + j; comp[nm] = min(prev[j] + cost, comp[nm]); } for (int j=0; j<m; ++j) { comp[j] = min(comp[j], comp[j+m]); } } prev = comp; for (int i=0; i<m*2; ++i) comp[i] = INF; } vector <ll> res(m); for (int i=0; i<m; ++i) { res[i] = prev[i]; } res[0] = 0; for (int i=1; i<m; ++i) { int oit = m; for (int p=0; p<m; ++p) { popr[p] = p; } for (int l=0; l<m; ++l) { int it = 0; for (int x=0; x<oit; ++x) { int j = popr[x]; int nm = (i + j)%m; if (res[nm] > res[j] + prev[i]) { res[nm] = res[j] + prev[i]; if (!kol[nm]) { npopr[it] = nm; it++; kol[nm] = true; } } } for (int p=0; p<it; ++p) { kol[npopr[p]] = false; popr[p] = npopr[p]; } oit = it; if (oit == 0) break; } } for (int i=0; i<m; ++i) { cout<<(res[i] >= INF ? -1 : res[i])<<'\n'; } } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); int t = 1; while (t--) { test(); } }
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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | #include <bits/stdc++.h> using namespace std; #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 #define ll long long #define all(x) x.begin(), x.end() #define pb push_back #define fi first #define se second typedef pair <int, int> pii; constexpr ll INF = 1e18+7; constexpr ll N = 14e3+7; int popr[N]; int npopr[N]; bool kol[N]; void test() { int n, k, m; cin>>n>>k>>m; vector <vector <pii> > Z(k, vector <pii>()); for (int i=0; i<n; ++i) { int a, b, c; cin>>a>>b>>c; --a; b %= m; Z[a].pb({b, c}); } vector <ll> comp(m*2, INF); vector <ll> prev(m*2, INF); prev[0] = 0; for (int col=0; col<k; ++col) { for (auto &[mass, cost] : Z[col]) { for (int j=m-1; j>=0; --j) { int nm = mass + j; comp[nm] = min(prev[j] + cost, comp[nm]); } for (int j=0; j<m; ++j) { comp[j] = min(comp[j], comp[j+m]); } } prev = comp; for (int i=0; i<m*2; ++i) comp[i] = INF; } vector <ll> res(m); for (int i=0; i<m; ++i) { res[i] = prev[i]; } res[0] = 0; for (int i=1; i<m; ++i) { int oit = m; for (int p=0; p<m; ++p) { popr[p] = p; } for (int l=0; l<m; ++l) { int it = 0; for (int x=0; x<oit; ++x) { int j = popr[x]; int nm = (i + j)%m; if (res[nm] > res[j] + prev[i]) { res[nm] = res[j] + prev[i]; if (!kol[nm]) { npopr[it] = nm; it++; kol[nm] = true; } } } for (int p=0; p<it; ++p) { kol[npopr[p]] = false; popr[p] = npopr[p]; } oit = it; if (oit == 0) break; } } for (int i=0; i<m; ++i) { cout<<(res[i] >= INF ? -1 : res[i])<<'\n'; } } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); int t = 1; while (t--) { test(); } } |