#pragma GCC optimize ("O3") #include <bits/stdc++.h> using namespace std; #define FOR(i, b, e) for(int i = (b); i < (e); i++) #define sz(x) int(x.size()) #define all(x) x.begin(), x.end() #define pb push_back #define mp make_pair #define st first #define nd second using ll = long long; using vi = vector<int>; using pii = pair<int, int>; 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 << "}"; } #ifdef LOCAL #define deb(x...) cerr << "[" #x "]: ", [](auto...$) { \ ((cerr << $ << "; "),...) << endl; }(x) #else #define deb(...) #endif const ll INF = 1e18; void solve() { int n, k, m; cin >> n >> k >> m; vector<vector<pii>> zel(k); FOR(i, 0, n) { int kk, mm, c; cin >> kk >> mm >> c; zel[kk-1].pb({mm, c}); } vector<ll> dp(m, INF); dp[0] = 0; FOR(col, 0, k) { vector<ll> nowy(m, INF); for(auto &[masa, c]: zel[col]) { int ind = masa % m; FOR(i, 0, m) { nowy[ind] = min(nowy[ind], dp[i] + c); ind++; if(ind == m) ind = 0; } } dp = nowy; } vector<ll> dist(m, INF); dist[0] = 0; vector<bool> vis(m); FOR(it, 0, m) { pair<ll, int> akt = {INF + 5, -1}; FOR(i, 0, m) if(!vis[i]) akt = min(akt, {dist[i], i}); int ind = akt.nd; ll d = dist[ind]; vis[ind] = 1; FOR(i, 0, m) { dist[ind] = min(dist[ind], d + dp[i]); ind++; if(ind == m) ind = 0; } } for(auto &x: dist) cout << (x == INF ? -1 : x) << '\n'; } int main() { cin.tie(0)->sync_with_stdio(0); int tt = 1; // cin >> tt; FOR(te, 0, 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 | #pragma GCC optimize ("O3") #include <bits/stdc++.h> using namespace std; #define FOR(i, b, e) for(int i = (b); i < (e); i++) #define sz(x) int(x.size()) #define all(x) x.begin(), x.end() #define pb push_back #define mp make_pair #define st first #define nd second using ll = long long; using vi = vector<int>; using pii = pair<int, int>; 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 << "}"; } #ifdef LOCAL #define deb(x...) cerr << "[" #x "]: ", [](auto...$) { \ ((cerr << $ << "; "),...) << endl; }(x) #else #define deb(...) #endif const ll INF = 1e18; void solve() { int n, k, m; cin >> n >> k >> m; vector<vector<pii>> zel(k); FOR(i, 0, n) { int kk, mm, c; cin >> kk >> mm >> c; zel[kk-1].pb({mm, c}); } vector<ll> dp(m, INF); dp[0] = 0; FOR(col, 0, k) { vector<ll> nowy(m, INF); for(auto &[masa, c]: zel[col]) { int ind = masa % m; FOR(i, 0, m) { nowy[ind] = min(nowy[ind], dp[i] + c); ind++; if(ind == m) ind = 0; } } dp = nowy; } vector<ll> dist(m, INF); dist[0] = 0; vector<bool> vis(m); FOR(it, 0, m) { pair<ll, int> akt = {INF + 5, -1}; FOR(i, 0, m) if(!vis[i]) akt = min(akt, {dist[i], i}); int ind = akt.nd; ll d = dist[ind]; vis[ind] = 1; FOR(i, 0, m) { dist[ind] = min(dist[ind], d + dp[i]); ind++; if(ind == m) ind = 0; } } for(auto &x: dist) cout << (x == INF ? -1 : x) << '\n'; } int main() { cin.tie(0)->sync_with_stdio(0); int tt = 1; // cin >> tt; FOR(te, 0, tt) solve(); return 0; } |