#include <bits/stdc++.h> using namespace std; typedef vector<int> VI; typedef pair <int,int> ii; typedef long long LL; #define pb push_back const LL INFF = 1LL << 61; const int N = 7005; int n, k, m, kk, mm, ww, i, j, j2, ind, a, b; LL tab[2][N], res[N], act; bool isEmpty; vector<ii> v[N]; int main() { scanf("%d %d %d", &n, &k, &m); while (n--) { scanf("%d %d %d", &kk, &mm, &ww); v[kk].pb(ii(mm, ww)); } printf("0\n"); isEmpty = false; for (i=1;i<=k;i++) if (v[i].size() == 0) isEmpty = true; if (isEmpty) { for (i=1;i<m;i++) printf("-1\n"); return 0; } a = 0; b = 1; tab[a][0] = 0; for (i=1;i<m;i++) tab[a][i] = INFF; for (i=1;i<=k;i++) { swap(a, b); for (j=0;j<m;j++) tab[a][j] = INFF; for (auto& w : v[i]) { for (j=0;j<m;j++) if (tab[b][j] != INFF) { j2 = j + w.first; if (j2 >= m) j2 -= m; if (tab[b][j] + w.second < tab[a][j2]) tab[a][j2] = tab[b][j] + w.second; } } } for (i=1;i<m;i++) if (tab[a][i] != INFF) { swap(a, b); for (j=0;j<m;j++) tab[a][j] = tab[b][j]; ind = i; act = tab[b][i]; while (act < INFF) { ind += i; if (ind >= m) ind -= m; if (ind == i) break; act += tab[b][i]; if (act < tab[a][ind]) tab[a][ind] = act; } } res[0] = 0; for (i=1;i<m;i++) res[i] = INFF; for (i=1;i<m;i++) if (tab[a][i] != INFF) { for (j=0;j<m;j++) tab[b][j] = res[j]; for (j=0;j<m;j++) if (res[j] != INFF) { j2 = j + i; if (j2 >= m) j2 -= m; if (res[j] + tab[a][i] < tab[b][j2]) tab[b][j2] = res[j] + tab[a][i]; } for (j=0;j<m;j++) res[j] = tab[b][j]; } for (i=1;i<m;i++) if (res[i] == INFF) printf("-1\n"); else printf("%lld\n", res[i]); 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 | #include <bits/stdc++.h> using namespace std; typedef vector<int> VI; typedef pair <int,int> ii; typedef long long LL; #define pb push_back const LL INFF = 1LL << 61; const int N = 7005; int n, k, m, kk, mm, ww, i, j, j2, ind, a, b; LL tab[2][N], res[N], act; bool isEmpty; vector<ii> v[N]; int main() { scanf("%d %d %d", &n, &k, &m); while (n--) { scanf("%d %d %d", &kk, &mm, &ww); v[kk].pb(ii(mm, ww)); } printf("0\n"); isEmpty = false; for (i=1;i<=k;i++) if (v[i].size() == 0) isEmpty = true; if (isEmpty) { for (i=1;i<m;i++) printf("-1\n"); return 0; } a = 0; b = 1; tab[a][0] = 0; for (i=1;i<m;i++) tab[a][i] = INFF; for (i=1;i<=k;i++) { swap(a, b); for (j=0;j<m;j++) tab[a][j] = INFF; for (auto& w : v[i]) { for (j=0;j<m;j++) if (tab[b][j] != INFF) { j2 = j + w.first; if (j2 >= m) j2 -= m; if (tab[b][j] + w.second < tab[a][j2]) tab[a][j2] = tab[b][j] + w.second; } } } for (i=1;i<m;i++) if (tab[a][i] != INFF) { swap(a, b); for (j=0;j<m;j++) tab[a][j] = tab[b][j]; ind = i; act = tab[b][i]; while (act < INFF) { ind += i; if (ind >= m) ind -= m; if (ind == i) break; act += tab[b][i]; if (act < tab[a][ind]) tab[a][ind] = act; } } res[0] = 0; for (i=1;i<m;i++) res[i] = INFF; for (i=1;i<m;i++) if (tab[a][i] != INFF) { for (j=0;j<m;j++) tab[b][j] = res[j]; for (j=0;j<m;j++) if (res[j] != INFF) { j2 = j + i; if (j2 >= m) j2 -= m; if (res[j] + tab[a][i] < tab[b][j2]) tab[b][j2] = res[j] + tab[a][i]; } for (j=0;j<m;j++) res[j] = tab[b][j]; } for (i=1;i<m;i++) if (res[i] == INFF) printf("-1\n"); else printf("%lld\n", res[i]); return 0; } |