#include <bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
long long n, k, m;
cin >> n >> k >> m;
vector<pair<int, pair<int, int>>> z;
for(int i = 0; i < n; i++){
int t1,t2,t3;
cin >> t1 >> t2 >> t3;
z.push_back({t1, {t2, t3}});
}
sort(z.begin(), z.end());
vector<int> a, b;
for(int i = 0; i < m; i++) a.push_back(-1);
b=a;
a[0]=0;
int ct = 1;
for(int i = 0; i < n; i++){
if(i>0&&z[i].first!=z[i-1].first){
a=b;
for(int j = 0; j < m; j++) b[j]=-1;
ct++;
}
int w = z[i].second.first;
int c = z[i].second.second;
for(int j = 0; j < m; j++){
if(a[j]==-1) continue;
if(b[(j+w)%m]==-1||a[j]+c<b[(j+w)%m]){
b[(j+w)%m]=a[j]+c;
}
}
}
if(ct!=k){
cout << "0\n";
for(int i = 1; i < m; i++) cout << "-1\n";
return 0;
}
a=b;
int fin[m];
for(int i = 0; i < m; i++) fin[i]=-1;
fin[0]=0;
vector<pair<int, int>> d;
priority_queue<pair<int, int>> q;
for(int i = 1; i < m; i++) if(a[i]!=-1) d.push_back({i, a[i]});
q.push({0,0});
while(!q.empty()){
int c=-q.top().first, l=q.top().second;
q.pop();
for(int i = 0; i < d.size(); i++){
if(fin[(l+d[i].first)%m]==-1||fin[(l+d[i].first)%m]>c+d[i].second){
fin[(l+d[i].first)%m]=c+d[i].second;
q.push({-(c+d[i].second), (l+d[i].first)%m});
}
}
}
for(int i = 0; i < m; i++) cout << fin[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 | #include <bits/stdc++.h> using namespace std; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, k, m; cin >> n >> k >> m; vector<pair<int, pair<int, int>>> z; for(int i = 0; i < n; i++){ int t1,t2,t3; cin >> t1 >> t2 >> t3; z.push_back({t1, {t2, t3}}); } sort(z.begin(), z.end()); vector<int> a, b; for(int i = 0; i < m; i++) a.push_back(-1); b=a; a[0]=0; int ct = 1; for(int i = 0; i < n; i++){ if(i>0&&z[i].first!=z[i-1].first){ a=b; for(int j = 0; j < m; j++) b[j]=-1; ct++; } int w = z[i].second.first; int c = z[i].second.second; for(int j = 0; j < m; j++){ if(a[j]==-1) continue; if(b[(j+w)%m]==-1||a[j]+c<b[(j+w)%m]){ b[(j+w)%m]=a[j]+c; } } } if(ct!=k){ cout << "0\n"; for(int i = 1; i < m; i++) cout << "-1\n"; return 0; } a=b; int fin[m]; for(int i = 0; i < m; i++) fin[i]=-1; fin[0]=0; vector<pair<int, int>> d; priority_queue<pair<int, int>> q; for(int i = 1; i < m; i++) if(a[i]!=-1) d.push_back({i, a[i]}); q.push({0,0}); while(!q.empty()){ int c=-q.top().first, l=q.top().second; q.pop(); for(int i = 0; i < d.size(); i++){ if(fin[(l+d[i].first)%m]==-1||fin[(l+d[i].first)%m]>c+d[i].second){ fin[(l+d[i].first)%m]=c+d[i].second; q.push({-(c+d[i].second), (l+d[i].first)%m}); } } } for(int i = 0; i < m; i++) cout << fin[i] << '\n'; return 0; } |
English