#include <bits/stdc++.h>
using namespace std;
#define st first
#define nd second
#define pb push_back
typedef long long ll;
const int N = 7e3+3;
const ll INF = 1e18;
vector<pair<int, ll>> klr[N];
ll dp[N], upd[N];
bool vctr[N];
int usd[N];
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, k, m;
cin >> n >> k >> m;
for(int i=0; i<n; i++){
int k, m, c;
cin >> k >> m >> c;
klr[k].pb({m, c});
}
int ms=0; ll cst=0;
for(int i=1; i<=k; i++){
if(klr[i].size()==0){
cout << 0 << '\n';
for(int j=1; j<m; j++) cout << -1 << '\n';
return 0;
}
ms+=klr[i][0].st;
ms%=m;
cst+=klr[i][0].nd;
}
for(int i=0; i<m; i++) dp[i]=INF;
dp[ms]=cst;
for(int i=1; i<=k; i++){
for(int j=0; j<m; j++) upd[j]=INF;
for(auto o:klr[i]){
for(int j=0; j<m; j++){
if(dp[j]==INF) continue;
int tmp=(j+m-klr[i][0].st+o.st)%m;
ll tmpC=dp[j]-klr[i][0].nd+o.nd;
upd[tmp]=min(upd[tmp], tmpC);
}
}
for(int j=0; j<m; j++) dp[j]=min(dp[j], upd[j]);
}
dp[0]=0;
queue<pair<int, ll>> Q;
int r=0;
for(int i=1; i<m; i++){
if(dp[i]<INF){
Q.push({i, dp[i]});
usd[r]=i;
r++;
vctr[i]=1;
}
}
while(!Q.empty()){
int cur=Q.front().st;
ll wg=Q.front().nd;
Q.pop();
if(dp[cur]<wg) continue;
int l=0;
while(l<r){
int w=usd[l];
int tmp=(w+cur)%m;
if(dp[tmp]>dp[cur]+dp[w]){
dp[tmp]=dp[cur]+dp[w];
if(vctr[tmp]==0){
usd[r]=tmp;
r++;
vctr[tmp]=1;
}
Q.push({tmp, dp[tmp]});
}
l++;
}
}
for(int i=0; i<m; i++){
if(dp[i]==INF) cout << -1 << '\n';
else cout << dp[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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | #include <bits/stdc++.h> using namespace std; #define st first #define nd second #define pb push_back typedef long long ll; const int N = 7e3+3; const ll INF = 1e18; vector<pair<int, ll>> klr[N]; ll dp[N], upd[N]; bool vctr[N]; int usd[N]; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n, k, m; cin >> n >> k >> m; for(int i=0; i<n; i++){ int k, m, c; cin >> k >> m >> c; klr[k].pb({m, c}); } int ms=0; ll cst=0; for(int i=1; i<=k; i++){ if(klr[i].size()==0){ cout << 0 << '\n'; for(int j=1; j<m; j++) cout << -1 << '\n'; return 0; } ms+=klr[i][0].st; ms%=m; cst+=klr[i][0].nd; } for(int i=0; i<m; i++) dp[i]=INF; dp[ms]=cst; for(int i=1; i<=k; i++){ for(int j=0; j<m; j++) upd[j]=INF; for(auto o:klr[i]){ for(int j=0; j<m; j++){ if(dp[j]==INF) continue; int tmp=(j+m-klr[i][0].st+o.st)%m; ll tmpC=dp[j]-klr[i][0].nd+o.nd; upd[tmp]=min(upd[tmp], tmpC); } } for(int j=0; j<m; j++) dp[j]=min(dp[j], upd[j]); } dp[0]=0; queue<pair<int, ll>> Q; int r=0; for(int i=1; i<m; i++){ if(dp[i]<INF){ Q.push({i, dp[i]}); usd[r]=i; r++; vctr[i]=1; } } while(!Q.empty()){ int cur=Q.front().st; ll wg=Q.front().nd; Q.pop(); if(dp[cur]<wg) continue; int l=0; while(l<r){ int w=usd[l]; int tmp=(w+cur)%m; if(dp[tmp]>dp[cur]+dp[w]){ dp[tmp]=dp[cur]+dp[w]; if(vctr[tmp]==0){ usd[r]=tmp; r++; vctr[tmp]=1; } Q.push({tmp, dp[tmp]}); } l++; } } for(int i=0; i<m; i++){ if(dp[i]==INF) cout << -1 << '\n'; else cout << dp[i] << '\n'; } return 0; } |
English