#include <vector>
#include <cstdio>
#include <stack>
#include <algorithm>
#include <queue>
#include <map>
#include <cmath>
#include <unordered_set>
#include <unordered_map>
#include <set>
#include <cstring>
#include <bitset>
#include <iostream>
#include <queue>
#include <iomanip>
#include <complex>
using namespace std;
#define ALL(x) x.begin(), x.end()
#define UNIQUE(c) (c).resize(unique(ALL(c)) - (c).begin())
#define FOR(i, a, b) for(int i =(a); i <=(b); ++i)
#define RE(i, n) FOR(i, 1, n)
#define RED(i, n) FORD(i, n, 1)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define REP(i, n) for(int i = 0;i <(n); ++i)
#define REPD(i, n) FORD(i, n-1,0)
const int maxn = 7012;
const long long mod = 998244353, inf = 1000000004000000004ll;
int n, ko, ma, k[maxn], m[maxn], ak, c[maxn];
vector<pair<long long, int> >z[maxn], ze;
long long dp[2][maxn], res[maxn];
bool akt = false, odw[maxn];
void solve() {
cin>>n>>ko>>ma;
REP(i, n){
cin>>k[i]>>m[i]>>c[i];
z[k[i]].emplace_back(c[i], m[i]);
}
REP(i, ma)dp[0][i] = dp[1][i] = -1;
dp[akt][0] = 0;
RE(i, ko)if(z[i].empty()) {
REP(j, ma)cout<<dp[0][j]<<"\n";
return;
}
RE(i, ko){
akt = !akt;
REP(j, ma)if(dp[!akt][j] > -1){
for(auto [cen, mas]: z[i]){
auto pom = j + mas;
if(pom >= ma)pom -= ma;
if(dp[akt][pom] == -1 || dp[!akt][j] + cen < dp[akt][pom]) {
dp[akt][pom] = dp[!akt][j] + cen;
}
}
}
REP(j, ma)dp[!akt][j] = -1;
}
REP(j, ma)if(dp[akt][j] > 0){
ze.emplace_back(dp[akt][j], j);
}
RE(i, ma)res[i] = -1;
REP(ij, ma){
ak = -1;
REP(i, ma) {
if(!odw[i] && res[i] > -1 && (ak == - 1 || res[ak] > res[i]))
ak = i;
}
if(ak == -1)break;
odw[ak] = true;
for(auto [cen, mas]: ze) {
auto pom = (mas + ak);
if(pom >= ma)pom -= ma;
if(res[pom] == -1 || res[ak] + cen < res[pom]) {
res[pom] = res[ak] + cen;
}
}
}
REP(i, ma){
cout<<res[i]<<"\n";
}
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(nullptr);
int tt = 1;
// cin >> tt;
while (tt--) {
solve();
}
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 94 95 96 | #include <vector> #include <cstdio> #include <stack> #include <algorithm> #include <queue> #include <map> #include <cmath> #include <unordered_set> #include <unordered_map> #include <set> #include <cstring> #include <bitset> #include <iostream> #include <queue> #include <iomanip> #include <complex> using namespace std; #define ALL(x) x.begin(), x.end() #define UNIQUE(c) (c).resize(unique(ALL(c)) - (c).begin()) #define FOR(i, a, b) for(int i =(a); i <=(b); ++i) #define RE(i, n) FOR(i, 1, n) #define RED(i, n) FORD(i, n, 1) #define FORD(i, a, b) for(int i = (a); i >= (b); --i) #define REP(i, n) for(int i = 0;i <(n); ++i) #define REPD(i, n) FORD(i, n-1,0) const int maxn = 7012; const long long mod = 998244353, inf = 1000000004000000004ll; int n, ko, ma, k[maxn], m[maxn], ak, c[maxn]; vector<pair<long long, int> >z[maxn], ze; long long dp[2][maxn], res[maxn]; bool akt = false, odw[maxn]; void solve() { cin>>n>>ko>>ma; REP(i, n){ cin>>k[i]>>m[i]>>c[i]; z[k[i]].emplace_back(c[i], m[i]); } REP(i, ma)dp[0][i] = dp[1][i] = -1; dp[akt][0] = 0; RE(i, ko)if(z[i].empty()) { REP(j, ma)cout<<dp[0][j]<<"\n"; return; } RE(i, ko){ akt = !akt; REP(j, ma)if(dp[!akt][j] > -1){ for(auto [cen, mas]: z[i]){ auto pom = j + mas; if(pom >= ma)pom -= ma; if(dp[akt][pom] == -1 || dp[!akt][j] + cen < dp[akt][pom]) { dp[akt][pom] = dp[!akt][j] + cen; } } } REP(j, ma)dp[!akt][j] = -1; } REP(j, ma)if(dp[akt][j] > 0){ ze.emplace_back(dp[akt][j], j); } RE(i, ma)res[i] = -1; REP(ij, ma){ ak = -1; REP(i, ma) { if(!odw[i] && res[i] > -1 && (ak == - 1 || res[ak] > res[i])) ak = i; } if(ak == -1)break; odw[ak] = true; for(auto [cen, mas]: ze) { auto pom = (mas + ak); if(pom >= ma)pom -= ma; if(res[pom] == -1 || res[ak] + cen < res[pom]) { res[pom] = res[ak] + cen; } } } REP(i, ma){ cout<<res[i]<<"\n"; } } int main() { ios_base::sync_with_stdio(false), cin.tie(nullptr); int tt = 1; // cin >> tt; while (tt--) { solve(); } return 0; } |
English