// Witold Milewski // PA 2024 #include <bits/stdc++.h> using namespace std; #define FOR(i, a, b) for(int i=a; i<=b; ++i) #define ll long long #define eb emplace_back #define pb push_back #define sz(A) (int)(A.size()) #define pi pair<int, int> #define f first #define s second const int maxn=7e3+1; const ll inf = 1e18; int N, K, M; vector<pi> con[maxn]; int main() { cin.tie(0) -> ios_base::sync_with_stdio(0); cin >> N >> K >> M; int k, m, c; FOR(i, 1, N) { cin >> k >> m >> c; con[k].pb({m, c}); } vector<ll> res(M); vector<vector<ll>> dp(2, vector<ll>(M, inf)); dp[0][0]=0; FOR(i, 0, K-1) { int a = i&1; int b = (i+1)&1; FOR(j, 0, M-1) dp[b][j]=inf; FOR(j, 0, M-1) { for(auto &[m, c] : con[i+1]) { dp[b][(j+m)%M]=min(dp[b][(j+m)%M], dp[a][j]+c); } } } FOR(i, 0, M-1) res[i]=dp[K&1][i]; res[0]=0; FOR(i, 1, M-1) { ll c = dp[K&1][i]; if(c==inf) continue; ll cena=c; int x=i; FOR(j, 2, M) { cena+=c; x+=i; if(x>=M) x-=M; if(cena>=inf) break; res[x]=min(res[x], cena); } } FOR(i, 0, 1) FOR(j, 0, M-1) dp[i][j]=inf; dp[1][0]=0; FOR(i, 1, M-1) { int a = i&1; int b = (i+1)&1; FOR(j, 0, M-1) dp[b][j]=inf; int cnt=i-1; FOR(j, 0, M-1) { ++cnt; if(cnt>=M) cnt-=M; dp[b][cnt]=min({dp[b][cnt], dp[a][j]+res[i], dp[a][cnt]}); } } int b = M&1; FOR(i, 0, M-1) { ll wyn=dp[b][i]; if(wyn==inf) cout << "-1\n"; else cout << wyn << '\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 68 69 70 71 72 73 74 75 76 77 | // Witold Milewski // PA 2024 #include <bits/stdc++.h> using namespace std; #define FOR(i, a, b) for(int i=a; i<=b; ++i) #define ll long long #define eb emplace_back #define pb push_back #define sz(A) (int)(A.size()) #define pi pair<int, int> #define f first #define s second const int maxn=7e3+1; const ll inf = 1e18; int N, K, M; vector<pi> con[maxn]; int main() { cin.tie(0) -> ios_base::sync_with_stdio(0); cin >> N >> K >> M; int k, m, c; FOR(i, 1, N) { cin >> k >> m >> c; con[k].pb({m, c}); } vector<ll> res(M); vector<vector<ll>> dp(2, vector<ll>(M, inf)); dp[0][0]=0; FOR(i, 0, K-1) { int a = i&1; int b = (i+1)&1; FOR(j, 0, M-1) dp[b][j]=inf; FOR(j, 0, M-1) { for(auto &[m, c] : con[i+1]) { dp[b][(j+m)%M]=min(dp[b][(j+m)%M], dp[a][j]+c); } } } FOR(i, 0, M-1) res[i]=dp[K&1][i]; res[0]=0; FOR(i, 1, M-1) { ll c = dp[K&1][i]; if(c==inf) continue; ll cena=c; int x=i; FOR(j, 2, M) { cena+=c; x+=i; if(x>=M) x-=M; if(cena>=inf) break; res[x]=min(res[x], cena); } } FOR(i, 0, 1) FOR(j, 0, M-1) dp[i][j]=inf; dp[1][0]=0; FOR(i, 1, M-1) { int a = i&1; int b = (i+1)&1; FOR(j, 0, M-1) dp[b][j]=inf; int cnt=i-1; FOR(j, 0, M-1) { ++cnt; if(cnt>=M) cnt-=M; dp[b][cnt]=min({dp[b][cnt], dp[a][j]+res[i], dp[a][cnt]}); } } int b = M&1; FOR(i, 0, M-1) { ll wyn=dp[b][i]; if(wyn==inf) cout << "-1\n"; else cout << wyn << '\n'; } } |