#include<bits/stdc++.h> #define ll long long #define ALL(x) begin(x), end(x) #define pil pair<int, ll> #define pb push_back #define fi first #define se second using namespace std; constexpr ll INF = 1000000000000000007; // 1e18 + 7 int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, k, m; cin >> n >> k >> m; vector <vector<pil> > v(k + 1); for (int i = 0; i < n; i++) { int a, b; ll c; cin >> a >> b >> c; v[a].pb({b, c}); } vector <ll> bp(m, INF); bp[0] = 0; for (int i = 1; i <= k; i++) { vector <ll> buff(m, INF); for (pil zelek : v[i]) { for (int j = 0; j < m; j++) buff[(j + zelek.fi) % m] = min(buff[(j + zelek.fi) % m], bp[j] + zelek.se); } swap(buff, bp); } vector <ll> bp2(m, INF); bp2[0] = 0; for (int i = 1; i < m; i++) { if (bp[i] == INF) continue; int num_steps = __gcd(i, m); for (int step = 0; step < num_steps; step++) { for (int j = step; (j + i) % m != step; j = (j + i) % m) bp2[(j + i) % m] = min(bp2[(j + i) % m], bp2[j] + bp[i]); bp2[step] = min(bp2[step], bp2[(step - i + m) % m] + bp[i]); for (int j = step; (j + i) % m != step; j = (j + i) % m) bp2[(j + i) % m] = min(bp2[(j + i) % m], bp2[j] + bp[i]); } } for (ll i : bp2) cout << (i == INF ? -1LL : 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 | #include<bits/stdc++.h> #define ll long long #define ALL(x) begin(x), end(x) #define pil pair<int, ll> #define pb push_back #define fi first #define se second using namespace std; constexpr ll INF = 1000000000000000007; // 1e18 + 7 int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, k, m; cin >> n >> k >> m; vector <vector<pil> > v(k + 1); for (int i = 0; i < n; i++) { int a, b; ll c; cin >> a >> b >> c; v[a].pb({b, c}); } vector <ll> bp(m, INF); bp[0] = 0; for (int i = 1; i <= k; i++) { vector <ll> buff(m, INF); for (pil zelek : v[i]) { for (int j = 0; j < m; j++) buff[(j + zelek.fi) % m] = min(buff[(j + zelek.fi) % m], bp[j] + zelek.se); } swap(buff, bp); } vector <ll> bp2(m, INF); bp2[0] = 0; for (int i = 1; i < m; i++) { if (bp[i] == INF) continue; int num_steps = __gcd(i, m); for (int step = 0; step < num_steps; step++) { for (int j = step; (j + i) % m != step; j = (j + i) % m) bp2[(j + i) % m] = min(bp2[(j + i) % m], bp2[j] + bp[i]); bp2[step] = min(bp2[step], bp2[(step - i + m) % m] + bp[i]); for (int j = step; (j + i) % m != step; j = (j + i) % m) bp2[(j + i) % m] = min(bp2[(j + i) % m], bp2[j] + bp[i]); } } for (ll i : bp2) cout << (i == INF ? -1LL : i) << "\n"; return 0; } |