#include <bits/stdc++.h> //#define int long long #define pii pair<int,int> #define all(x) x.begin(),x.end() #define vi vector<int> #define vii vector<pii> #define vb vector<bool> #define siz(x) (int)x.size() #define pb push_back #define nd second #define st first #define rep(i,a,b) for(int i=a; i<=b; i++) using namespace std; const long long inf = 1e18; int32_t main(){ ios_base::sync_with_stdio(0); cin.tie(0); int N,M,K; cin>>N>>K>>M; map<pair<int,int>,int>z; // {{kolor,masa},koszt} rep(i,1,N){ int k,m,c; cin>>k>>m>>c; if(z.count({k,m})){ if(z[{k,m}]>c)z[{k,m}] = c; }else z[{k,m}] = c; } vector<vii>zelki(K+1); for(auto [a,c] : z){ zelki[a.st].pb({a.nd,c}); } vector<long long> dp(M,inf); dp[0] = 0; rep(k,1,K){ vector<long long> ndp(M,inf); rep(i,0,M-1){ if(dp[i]!=inf) for(auto [m,c] : zelki[k]){ ndp[(i+m)%M] = min(ndp[(i+m)%M],dp[i]+c); } } dp=ndp; } dp[0] = 0; while(1){ bool zmiana = false; vector<long long>ndp(M,inf), ost=dp; rep(i,0,M-1)if(dp[i]!=inf){ rep(j,0,M-1)if(dp[j]!=inf){ if(ndp[(i+j)%M]>dp[i]+dp[j]){ ndp[(i+j)%M] = dp[i]+dp[j]; } } } rep(i,0,M-1)dp[i] = min(dp[i],ndp[i]); if(ost == dp)break; } for(auto w : dp){if(w==inf)cout<<"-1\n";else cout<<w<<'\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 | #include <bits/stdc++.h> //#define int long long #define pii pair<int,int> #define all(x) x.begin(),x.end() #define vi vector<int> #define vii vector<pii> #define vb vector<bool> #define siz(x) (int)x.size() #define pb push_back #define nd second #define st first #define rep(i,a,b) for(int i=a; i<=b; i++) using namespace std; const long long inf = 1e18; int32_t main(){ ios_base::sync_with_stdio(0); cin.tie(0); int N,M,K; cin>>N>>K>>M; map<pair<int,int>,int>z; // {{kolor,masa},koszt} rep(i,1,N){ int k,m,c; cin>>k>>m>>c; if(z.count({k,m})){ if(z[{k,m}]>c)z[{k,m}] = c; }else z[{k,m}] = c; } vector<vii>zelki(K+1); for(auto [a,c] : z){ zelki[a.st].pb({a.nd,c}); } vector<long long> dp(M,inf); dp[0] = 0; rep(k,1,K){ vector<long long> ndp(M,inf); rep(i,0,M-1){ if(dp[i]!=inf) for(auto [m,c] : zelki[k]){ ndp[(i+m)%M] = min(ndp[(i+m)%M],dp[i]+c); } } dp=ndp; } dp[0] = 0; while(1){ bool zmiana = false; vector<long long>ndp(M,inf), ost=dp; rep(i,0,M-1)if(dp[i]!=inf){ rep(j,0,M-1)if(dp[j]!=inf){ if(ndp[(i+j)%M]>dp[i]+dp[j]){ ndp[(i+j)%M] = dp[i]+dp[j]; } } } rep(i,0,M-1)dp[i] = min(dp[i],ndp[i]); if(ost == dp)break; } for(auto w : dp){if(w==inf)cout<<"-1\n";else cout<<w<<'\n';} } |