#include <bits/stdc++.h> using namespace std; typedef long long int lli; const int MAXN = 7005; vector<pair<int, lli>> kol[MAXN]; int unsafe_mod(int x, int m) { if (x >= m) x -= m; while (x >= m) x -= m; return x; } lli res[MAXN]; lli new_val[MAXN]; vector<int> active; vector<int> moved; int main() { int n, k, m; scanf("%d%d%d", &n, &k, &m); for (int i=0; i<=m; i++) { res[i] = -1; new_val[i] = -1; } res[0] = 0; // new_val[0] = 0; // active.reserve(3*m); // moved.reserve(100*m); // active.push_back(0); int ki, mi, ci; for (int i=0; i<n; i++) { scanf("%d%d%d", &ki, &mi, &ci); kol[ki].push_back({mi, ci}); } for (int i=1; i<=k; i++) { if (kol[i].empty()) { for (int j=0; j<m; j++) printf("%lld\n", res[j]); return 0; } } for (int it=1; it<=k; it++) { if (kol[it].empty()) continue; // moved.clear(); for (const auto &p : kol[it]) { int mas = p.first; lli cost = p.second; int tar; // for (int x : active) { for (int x=0; x<m; x++) { if (res[x] == -1) continue; tar = unsafe_mod(x + mas, m); if (new_val[tar] == -1 || new_val[tar] > res[x] + cost) { new_val[tar] = res[x] + cost; } } } for (int i=0; i<m; i++) { res[i] = new_val[i]; new_val[i] = -1; } } // for (int i=0; i<m; i++) { // printf(" %lld\n", res[i]); // } res[0] = 0; for (int i=1; i<m; i++) { if (res[i] == -1) continue; int n_vis = 0; int tar; for (int st=0; n_vis < 2*m; st++) { // lecimy od st int cur = st; // printf("start: %d\n", st); for (int tmp=0; tmp < 2; tmp++) { do { // printf("vis: %d\n", cur); n_vis++; if (res[cur] == -1) { cur += i; if (cur >= m) cur -= m; continue; } tar = unsafe_mod(cur+i, m); if (res[tar] == -1 || res[tar] > res[cur] + res[i]) res[tar] = res[cur] + res[i]; cur += i; if (cur >= m) cur -= m; } while (cur != st); } } assert(n_vis == 2*m); } for (int i=0; i<m; i++) { printf("%lld\n", res[i]); } }
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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | #include <bits/stdc++.h> using namespace std; typedef long long int lli; const int MAXN = 7005; vector<pair<int, lli>> kol[MAXN]; int unsafe_mod(int x, int m) { if (x >= m) x -= m; while (x >= m) x -= m; return x; } lli res[MAXN]; lli new_val[MAXN]; vector<int> active; vector<int> moved; int main() { int n, k, m; scanf("%d%d%d", &n, &k, &m); for (int i=0; i<=m; i++) { res[i] = -1; new_val[i] = -1; } res[0] = 0; // new_val[0] = 0; // active.reserve(3*m); // moved.reserve(100*m); // active.push_back(0); int ki, mi, ci; for (int i=0; i<n; i++) { scanf("%d%d%d", &ki, &mi, &ci); kol[ki].push_back({mi, ci}); } for (int i=1; i<=k; i++) { if (kol[i].empty()) { for (int j=0; j<m; j++) printf("%lld\n", res[j]); return 0; } } for (int it=1; it<=k; it++) { if (kol[it].empty()) continue; // moved.clear(); for (const auto &p : kol[it]) { int mas = p.first; lli cost = p.second; int tar; // for (int x : active) { for (int x=0; x<m; x++) { if (res[x] == -1) continue; tar = unsafe_mod(x + mas, m); if (new_val[tar] == -1 || new_val[tar] > res[x] + cost) { new_val[tar] = res[x] + cost; } } } for (int i=0; i<m; i++) { res[i] = new_val[i]; new_val[i] = -1; } } // for (int i=0; i<m; i++) { // printf(" %lld\n", res[i]); // } res[0] = 0; for (int i=1; i<m; i++) { if (res[i] == -1) continue; int n_vis = 0; int tar; for (int st=0; n_vis < 2*m; st++) { // lecimy od st int cur = st; // printf("start: %d\n", st); for (int tmp=0; tmp < 2; tmp++) { do { // printf("vis: %d\n", cur); n_vis++; if (res[cur] == -1) { cur += i; if (cur >= m) cur -= m; continue; } tar = unsafe_mod(cur+i, m); if (res[tar] == -1 || res[tar] > res[cur] + res[i]) res[tar] = res[cur] + res[i]; cur += i; if (cur >= m) cur -= m; } while (cur != st); } } assert(n_vis == 2*m); } for (int i=0; i<m; i++) { printf("%lld\n", res[i]); } } |