#include <iostream>
#include <vector>
#include <algorithm>
#include <limits>
#include <set>
using namespace std;
bool odw[7007];
long long t[7007], ta[7007], dp[7007];
vector <long long> V[7007];
vector<long long > V2[7007];
long long n, k, m, a, b, c, counter=0;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> k >> m;
for(int i=0;i<m;i++){
t[i]=1e18;
ta[i]=1e18;
}
for(int i=0;i<n;i++){
cin>>a>>b>>c;
if(!odw[a]){
counter++;
odw[a]=true;
}
V[i].push_back(a);
V[i].push_back(b);
V[i].push_back(c);
}
if(counter != k){
cout<<0<<endl;
for(int i=1;i<m;i++){
cout<<-1<<endl;
}
return 0;
}
sort(V,V+n);
ta[0] = 0;
t[V[0][1]%m] = V[0][2];
for(int i=1;i<n;i++){
if(V[i][0] != V[i-1][0]){
for(int j=0;j<m;j++){
ta[j] = t[j];
t[j] = 1e18;
}
}
for(int j=0;j<m;j++){
if(ta[(j-V[i][1]+m)%m] != 1e18){
t[j] = min(t[j], ta[(j-V[i][1]+m)%m]+V[i][2]);
}
}
}
for(int j=1;j<m;j++){
for(int i=1;i<j;i++){
if(t[(j-i+m)%m] != 1e18){
t[j] = min(t[j], t[(j-i+m)%m]+t[i]);
}
}
}
int iterator=0;
for(int i=0;i<m;i++){
if(t[i]!=1e18){
V2[iterator].push_back(i);
V2[iterator].push_back(t[i]);
iterator++;
}
t[i]=1e18;
}
t[0] = 0;
for(int j=0;j<iterator;j++){
for(int i=0;i<m;i++){
t[(V2[j][0]*i)%m] = min(t[(V2[j][0]*i)%m], V2[j][1]*i);
}
}
for(int j=0;j<iterator;j++){
for(int i=0;i<m;i++){
if(t[i%m]!=1e18){
t[(i+V2[j][0])%m] = min(t[i%m] + V2[j][1], t[(i+V2[j][0])%m]);
}
}
}
for(int i=0;i<m;i++){
if(t[i] == 1e18){
cout<<-1<<"\n";
}
else{
cout<<t[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 88 89 90 91 92 93 | #include <iostream> #include <vector> #include <algorithm> #include <limits> #include <set> using namespace std; bool odw[7007]; long long t[7007], ta[7007], dp[7007]; vector <long long> V[7007]; vector<long long > V2[7007]; long long n, k, m, a, b, c, counter=0; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> k >> m; for(int i=0;i<m;i++){ t[i]=1e18; ta[i]=1e18; } for(int i=0;i<n;i++){ cin>>a>>b>>c; if(!odw[a]){ counter++; odw[a]=true; } V[i].push_back(a); V[i].push_back(b); V[i].push_back(c); } if(counter != k){ cout<<0<<endl; for(int i=1;i<m;i++){ cout<<-1<<endl; } return 0; } sort(V,V+n); ta[0] = 0; t[V[0][1]%m] = V[0][2]; for(int i=1;i<n;i++){ if(V[i][0] != V[i-1][0]){ for(int j=0;j<m;j++){ ta[j] = t[j]; t[j] = 1e18; } } for(int j=0;j<m;j++){ if(ta[(j-V[i][1]+m)%m] != 1e18){ t[j] = min(t[j], ta[(j-V[i][1]+m)%m]+V[i][2]); } } } for(int j=1;j<m;j++){ for(int i=1;i<j;i++){ if(t[(j-i+m)%m] != 1e18){ t[j] = min(t[j], t[(j-i+m)%m]+t[i]); } } } int iterator=0; for(int i=0;i<m;i++){ if(t[i]!=1e18){ V2[iterator].push_back(i); V2[iterator].push_back(t[i]); iterator++; } t[i]=1e18; } t[0] = 0; for(int j=0;j<iterator;j++){ for(int i=0;i<m;i++){ t[(V2[j][0]*i)%m] = min(t[(V2[j][0]*i)%m], V2[j][1]*i); } } for(int j=0;j<iterator;j++){ for(int i=0;i<m;i++){ if(t[i%m]!=1e18){ t[(i+V2[j][0])%m] = min(t[i%m] + V2[j][1], t[(i+V2[j][0])%m]); } } } for(int i=0;i<m;i++){ if(t[i] == 1e18){ cout<<-1<<"\n"; } else{ cout<<t[i]<<"\n"; } } return 0; } |
English