#pragma GCC optimize ("O3") #include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i = (a); i <= (b); i++) #define per(i, a, b) for (int i = (b); i >= (a); i--) #define SZ(x) ((int)x.size()) #define all(x) x.begin(), x.end() #define pb push_back #define mp make_pair #define mt make_tuple #define st first #define nd second using ll = long long; using vi = vector<int>; using pii = pair<int, int>; using pll = pair<ll, ll>; auto &operator<<(auto &o, pair<auto, auto> p) { return o << "(" << p.st << ", " << p.nd << ")"; } auto operator<<(auto &o, auto x)->decltype(end(x), o) { o << "{"; int i=0; for(auto e: x) o << ", " + 2*!i++ << e; return o << "}"; } #define deb(x...) cerr << "[" #x "]: ", [](auto...$) { ((cerr<<$<<"; "),...) << endl; }(x) const int nax = 7777; int n, k, m; vector<pii> kolor[nax]; ll dp[nax][nax]; const ll inf = 1e18; void upd(ll &x, ll y){ x = min(x, y); } ll dist[nax]; bool vis[nax]; void solve(){ cin >> n >> k >> m; rep(i, 1, n){ int c; cin >> c; int wei, cost; cin >> wei >> cost; kolor[c].pb({wei, cost}); } rep(i, 0, k){ rep(j, 0, nax - 1) dp[i][j] = inf; } dp[0][0] = 0; rep(i, 0, k - 1){ for(auto [wei, cost] : kolor[i + 1]){ for(int rem=0;rem<m;rem++){ upd(dp[i + 1][(rem + wei) % m], dp[i][rem] + cost); } } } rep(i, 0, m - 1) dist[i] = inf; dist[0] = 0; rep(runda, 1, m){ ll mincost = inf + inf; int id = -1; rep(i, 0, m - 1){ if(!vis[i] && dist[i] < mincost){ mincost = dist[i]; id = i; } } vis[id] = true; rep(i, 0, m - 1){ upd(dist[(id + i) % m], dist[id] + dp[k][i]); } } rep(i, 0, m - 1){ ll cur = dist[i]; if(cur == inf) cur = -1; cout << cur << "\n"; } } int main() { ios::sync_with_stdio(0); cin.tie(0); int tt = 1; // cin >> tt; rep(te, 1, tt) solve(); 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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | #pragma GCC optimize ("O3") #include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i = (a); i <= (b); i++) #define per(i, a, b) for (int i = (b); i >= (a); i--) #define SZ(x) ((int)x.size()) #define all(x) x.begin(), x.end() #define pb push_back #define mp make_pair #define mt make_tuple #define st first #define nd second using ll = long long; using vi = vector<int>; using pii = pair<int, int>; using pll = pair<ll, ll>; auto &operator<<(auto &o, pair<auto, auto> p) { return o << "(" << p.st << ", " << p.nd << ")"; } auto operator<<(auto &o, auto x)->decltype(end(x), o) { o << "{"; int i=0; for(auto e: x) o << ", " + 2*!i++ << e; return o << "}"; } #define deb(x...) cerr << "[" #x "]: ", [](auto...$) { ((cerr<<$<<"; "),...) << endl; }(x) const int nax = 7777; int n, k, m; vector<pii> kolor[nax]; ll dp[nax][nax]; const ll inf = 1e18; void upd(ll &x, ll y){ x = min(x, y); } ll dist[nax]; bool vis[nax]; void solve(){ cin >> n >> k >> m; rep(i, 1, n){ int c; cin >> c; int wei, cost; cin >> wei >> cost; kolor[c].pb({wei, cost}); } rep(i, 0, k){ rep(j, 0, nax - 1) dp[i][j] = inf; } dp[0][0] = 0; rep(i, 0, k - 1){ for(auto [wei, cost] : kolor[i + 1]){ for(int rem=0;rem<m;rem++){ upd(dp[i + 1][(rem + wei) % m], dp[i][rem] + cost); } } } rep(i, 0, m - 1) dist[i] = inf; dist[0] = 0; rep(runda, 1, m){ ll mincost = inf + inf; int id = -1; rep(i, 0, m - 1){ if(!vis[i] && dist[i] < mincost){ mincost = dist[i]; id = i; } } vis[id] = true; rep(i, 0, m - 1){ upd(dist[(id + i) % m], dist[id] + dp[k][i]); } } rep(i, 0, m - 1){ ll cur = dist[i]; if(cur == inf) cur = -1; cout << cur << "\n"; } } int main() { ios::sync_with_stdio(0); cin.tie(0); int tt = 1; // cin >> tt; rep(te, 1, tt) solve(); return 0; } |