#include <bits/stdc++.h> #define fi first #define sc second #define pb push_back #define rep(i,p,k) for(int i=(p);i<(k);++i) #define forn(i,p,k) for(int i=(p);i<=(k);++i) #define forr(i,k,p) for(int i=(k)-1;i>=(p);--i) #define each(a,b) for(auto&a:b) #define all(v) begin(v), end(v) #define RET(smth) return void(cout<<(smth)<<'\n'); #define sz(v) (int)v.size() using namespace std; using pii = pair<int,int>; using ll = long long; using Vi = vector<int>; #ifdef DEBUG #include "debug.h" #else #define debug(...) ; #endif const ll inf = 1e18; int main() { #ifndef DEBUG cin.tie(0) -> sync_with_stdio(0); #endif int n,k,m; cin >> n >> k >> m; auto const mod = [m](int a) { return a >= m ? a-m : a; }; vector<Vi> K(k); vector<pii> T(n); rep(i,0,n) { int ki; cin >> ki >> T[i].fi >> T[i].sc; if(T[i].fi == m) T[i].fi = 0; K[ki-1].push_back(i); } vector<ll> dp(m), prev(m, inf); prev[0] = 0; for(auto & V : K) { fill(all(dp), inf); for(auto ind : V) { auto [ms,c] = T[ind]; rep(i,0,m) dp[i] = min(dp[i], prev[mod(i - ms + m)] + c); } dp.swap(prev); } vector<ll> dist(m, inf); dist[0] = 0; Vi vis(m, 0); while(1) { ll mm = inf; int ind = -1; rep(i,0,m) if(!vis[i] && mm > dist[i]) { ind = i; mm = dist[i]; } if(ind == -1) break; vis[ind] = 1; rep(i,0,m) { int tar = mod(ind + i); dist[tar] = min(dist[tar], mm + prev[i]); } } rep(i,0,m) cout << (dist[i] == inf ? -1 : dist[i]) << '\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 | #include <bits/stdc++.h> #define fi first #define sc second #define pb push_back #define rep(i,p,k) for(int i=(p);i<(k);++i) #define forn(i,p,k) for(int i=(p);i<=(k);++i) #define forr(i,k,p) for(int i=(k)-1;i>=(p);--i) #define each(a,b) for(auto&a:b) #define all(v) begin(v), end(v) #define RET(smth) return void(cout<<(smth)<<'\n'); #define sz(v) (int)v.size() using namespace std; using pii = pair<int,int>; using ll = long long; using Vi = vector<int>; #ifdef DEBUG #include "debug.h" #else #define debug(...) ; #endif const ll inf = 1e18; int main() { #ifndef DEBUG cin.tie(0) -> sync_with_stdio(0); #endif int n,k,m; cin >> n >> k >> m; auto const mod = [m](int a) { return a >= m ? a-m : a; }; vector<Vi> K(k); vector<pii> T(n); rep(i,0,n) { int ki; cin >> ki >> T[i].fi >> T[i].sc; if(T[i].fi == m) T[i].fi = 0; K[ki-1].push_back(i); } vector<ll> dp(m), prev(m, inf); prev[0] = 0; for(auto & V : K) { fill(all(dp), inf); for(auto ind : V) { auto [ms,c] = T[ind]; rep(i,0,m) dp[i] = min(dp[i], prev[mod(i - ms + m)] + c); } dp.swap(prev); } vector<ll> dist(m, inf); dist[0] = 0; Vi vis(m, 0); while(1) { ll mm = inf; int ind = -1; rep(i,0,m) if(!vis[i] && mm > dist[i]) { ind = i; mm = dist[i]; } if(ind == -1) break; vis[ind] = 1; rep(i,0,m) { int tar = mod(ind + i); dist[tar] = min(dist[tar], mm + prev[i]); } } rep(i,0,m) cout << (dist[i] == inf ? -1 : dist[i]) << '\n'; return 0; } |