#include <bits/stdc++.h> using namespace std; #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return {i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (c it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define imie(x...) " [" #x ": " << (x) << "] " #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> template <typename A, typename B> using unordered_map2 = __gnu_pbds::gp_hash_table<A, B>; using namespace __gnu_pbds; template <typename T> using ordered_set = __gnu_pbds::tree<T, __gnu_pbds::null_type, less<T>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>; // ordered_set<int> s; s.insert(1); s.insert(2); // s.order_of_key(1); // Out: 0. // *s.find_by_order(1); // Out: 2. using ld = long double; using ll = long long; constexpr int mod = 1000 * 1000 * 1000 + 7; constexpr int odw2 = (mod + 1) / 2; void OdejmijOd(int& a, int b) { a -= b; if (a < 0) a += mod; } int Odejmij(int a, int b) { OdejmijOd(a, b); return a; } void DodajDo(int& a, int b) { a += b; if (a >= mod) a -= mod; } int Dodaj(int a, int b) { DodajDo(a, b); return a; } int Mnoz(int a, int b) { return (ll) a * b % mod; } void MnozDo(int& a, int b) { a = Mnoz(a, b); } int Pot(int a, ll b) { int res = 1; while (b) { if (b % 2 == 1) MnozDo(res, a); a = Mnoz(a, a); b /= 2; } return res; } int Odw(int a) { return Pot(a, mod - 2); } void PodzielDo(int& a, int b) { MnozDo(a, Odw(b)); } int Podziel(int a, int b) { return Mnoz(a, Odw(b)); } int Moduluj(ll x) { x %= mod; if (x < 0) x += mod; return x; } template <typename T> T Maxi(T& a, T b) { return a = max(a, b); } template <typename T> T Mini(T& a, T b) { return a = min(a, b); } constexpr ll infty = std::numeric_limits<ll>::max() / 2; struct Zelek { int m, c; }; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, k, m; cin >> n >> k >> m; map<int, vector<Zelek>> zelki; for (int i = 0; i < n; i++) { Zelek zelek; int ki; cin >> ki >> zelek.m >> zelek.c; zelki[ki].push_back(zelek); } vector<ll> koszty(m, infty); koszty[0] = 0; for (int ki = 1; ki <= k; ki++) { vector<ll> nowe_koszty(m, infty); for (const Zelek& z : zelki[ki]) { for (int r = 0; r < m; r++) { Mini(nowe_koszty[(r + z.m) % m], koszty[r] + z.c); } } swap(koszty, nowe_koszty); } vector<ll> wynik(m, infty); wynik[0] = 0; set<pair<ll, int>> kol; kol.insert(make_pair(wynik[0], 0)); while (!kol.empty()) { const ll odl = kol.begin()->first; const int w = kol.begin()->second; kol.erase(kol.begin()); assert(odl == wynik[w]); for (int i = 0; i < m; i++) { const int j = (i + w) % m; const ll new_odl = odl + koszty[i]; if (wynik[j] > new_odl) { kol.erase(make_pair(wynik[j], j)); wynik[j] = new_odl; kol.insert(make_pair(wynik[j], j)); } } } for (ll x : wynik) { if (x == infty) { x = -1; } cout << x << "\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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | #include <bits/stdc++.h> using namespace std; #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return {i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (c it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define imie(x...) " [" #x ": " << (x) << "] " #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> template <typename A, typename B> using unordered_map2 = __gnu_pbds::gp_hash_table<A, B>; using namespace __gnu_pbds; template <typename T> using ordered_set = __gnu_pbds::tree<T, __gnu_pbds::null_type, less<T>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>; // ordered_set<int> s; s.insert(1); s.insert(2); // s.order_of_key(1); // Out: 0. // *s.find_by_order(1); // Out: 2. using ld = long double; using ll = long long; constexpr int mod = 1000 * 1000 * 1000 + 7; constexpr int odw2 = (mod + 1) / 2; void OdejmijOd(int& a, int b) { a -= b; if (a < 0) a += mod; } int Odejmij(int a, int b) { OdejmijOd(a, b); return a; } void DodajDo(int& a, int b) { a += b; if (a >= mod) a -= mod; } int Dodaj(int a, int b) { DodajDo(a, b); return a; } int Mnoz(int a, int b) { return (ll) a * b % mod; } void MnozDo(int& a, int b) { a = Mnoz(a, b); } int Pot(int a, ll b) { int res = 1; while (b) { if (b % 2 == 1) MnozDo(res, a); a = Mnoz(a, a); b /= 2; } return res; } int Odw(int a) { return Pot(a, mod - 2); } void PodzielDo(int& a, int b) { MnozDo(a, Odw(b)); } int Podziel(int a, int b) { return Mnoz(a, Odw(b)); } int Moduluj(ll x) { x %= mod; if (x < 0) x += mod; return x; } template <typename T> T Maxi(T& a, T b) { return a = max(a, b); } template <typename T> T Mini(T& a, T b) { return a = min(a, b); } constexpr ll infty = std::numeric_limits<ll>::max() / 2; struct Zelek { int m, c; }; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, k, m; cin >> n >> k >> m; map<int, vector<Zelek>> zelki; for (int i = 0; i < n; i++) { Zelek zelek; int ki; cin >> ki >> zelek.m >> zelek.c; zelki[ki].push_back(zelek); } vector<ll> koszty(m, infty); koszty[0] = 0; for (int ki = 1; ki <= k; ki++) { vector<ll> nowe_koszty(m, infty); for (const Zelek& z : zelki[ki]) { for (int r = 0; r < m; r++) { Mini(nowe_koszty[(r + z.m) % m], koszty[r] + z.c); } } swap(koszty, nowe_koszty); } vector<ll> wynik(m, infty); wynik[0] = 0; set<pair<ll, int>> kol; kol.insert(make_pair(wynik[0], 0)); while (!kol.empty()) { const ll odl = kol.begin()->first; const int w = kol.begin()->second; kol.erase(kol.begin()); assert(odl == wynik[w]); for (int i = 0; i < m; i++) { const int j = (i + w) % m; const ll new_odl = odl + koszty[i]; if (wynik[j] > new_odl) { kol.erase(make_pair(wynik[j], j)); wynik[j] = new_odl; kol.insert(make_pair(wynik[j], j)); } } } for (ll x : wynik) { if (x == infty) { x = -1; } cout << x << "\n"; } return 0; } |