#include <bits/stdc++.h> using namespace std; typedef long long ll; struct Typ { ll k, m, c; bool operator<(const Typ& b) const { return k < b.k; } }; const ll MAX = 7007; vector<ll> paczki; ll odl[MAX]; bool odw[MAX]; void generuj(ll n, ll m, ll k, vector<Typ>& typy) { vector<ll> stare(m, INT_MAX); vector<ll> nowe(m, INT_MAX); paczki = vector<ll>(m, INT_MAX); ll last = 0; stare[0] = 0; nowe[0] = 0; for (ll i = 0; i < n; i++) { if (typy[i].k > last+1) return; if (typy[i].k == last+1) { last = typy[i].k; swap(stare, nowe); nowe = vector<ll>(m, INT_MAX); } for (ll j = 0; j < m; j++) { if (stare[j] == INT_MAX) continue; ll noweM = (j + typy[i].m) % m; ll noweC = stare[j] + typy[i].c; nowe[noweM] = min(nowe[noweM], noweC); } } if (last < k) return; paczki = nowe; } int main(void) { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ll n, k, m; cin >> n >> k >> m; vector<Typ> typy(n); for (ll i = 0; i < n; i++) cin >> typy[i].k >> typy[i].m >> typy[i].c; sort(typy.begin(), typy.end()); if (k > n) { cout << "0\n"; for (ll i = 1; i <= m-1; i++) cout << "-1\n"; return 0; } generuj(n, m, k, typy); odl[0] = 0; for (ll i = 1; i < m; i++) odl[i] = INT_MAX; while (true) { ll mini = -1; for (ll j = 0; j < m; j++) { if (odw[j]) continue; if (mini == -1 or odl[j] < odl[mini]) mini = j; } if (mini == -1 or odl[mini] == INT_MAX) break; odw[mini] = true; for (ll i = 0; i < m; i++) { if (paczki[i] == INT_MAX) continue; ll noweM = (mini + i) % m; ll noweC = odl[mini] + paczki[i]; if (odw[noweM] or odl[noweM] < noweC) continue; odl[noweM] = noweC; } } for (ll i = 0; i < m; i++) { if (odl[i] == INT_MAX) { cout << "-1\n"; continue; } cout << odl[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 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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; struct Typ { ll k, m, c; bool operator<(const Typ& b) const { return k < b.k; } }; const ll MAX = 7007; vector<ll> paczki; ll odl[MAX]; bool odw[MAX]; void generuj(ll n, ll m, ll k, vector<Typ>& typy) { vector<ll> stare(m, INT_MAX); vector<ll> nowe(m, INT_MAX); paczki = vector<ll>(m, INT_MAX); ll last = 0; stare[0] = 0; nowe[0] = 0; for (ll i = 0; i < n; i++) { if (typy[i].k > last+1) return; if (typy[i].k == last+1) { last = typy[i].k; swap(stare, nowe); nowe = vector<ll>(m, INT_MAX); } for (ll j = 0; j < m; j++) { if (stare[j] == INT_MAX) continue; ll noweM = (j + typy[i].m) % m; ll noweC = stare[j] + typy[i].c; nowe[noweM] = min(nowe[noweM], noweC); } } if (last < k) return; paczki = nowe; } int main(void) { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ll n, k, m; cin >> n >> k >> m; vector<Typ> typy(n); for (ll i = 0; i < n; i++) cin >> typy[i].k >> typy[i].m >> typy[i].c; sort(typy.begin(), typy.end()); if (k > n) { cout << "0\n"; for (ll i = 1; i <= m-1; i++) cout << "-1\n"; return 0; } generuj(n, m, k, typy); odl[0] = 0; for (ll i = 1; i < m; i++) odl[i] = INT_MAX; while (true) { ll mini = -1; for (ll j = 0; j < m; j++) { if (odw[j]) continue; if (mini == -1 or odl[j] < odl[mini]) mini = j; } if (mini == -1 or odl[mini] == INT_MAX) break; odw[mini] = true; for (ll i = 0; i < m; i++) { if (paczki[i] == INT_MAX) continue; ll noweM = (mini + i) % m; ll noweC = odl[mini] + paczki[i]; if (odw[noweM] or odl[noweM] < noweC) continue; odl[noweM] = noweC; } } for (ll i = 0; i < m; i++) { if (odl[i] == INT_MAX) { cout << "-1\n"; continue; } cout << odl[i] << '\n'; } return 0; } |