#include <bits/stdc++.h> using namespace std; #define int long long #define ALL(x) (x).begin(), (x).end() #define FOR(i, a, b) for(int i = a; i <= b; i++) #define FORD(i, a, b) for(int i = a; i >= b; i--) #define REP(i, n) for(int i = 0; i < n; i++) #define VI vector<int> #define PII pair<int,int> #define SZ(x) (int)((x).size()) #define PB push_back #define MP make_pair #define st first #define nd second template<class C> void mini(C& _a4, C _b4) { _a4 = min(_a4, _b4); } template<class C> void maxi(C& _a4, C _b4) { _a4 = max(_a4, _b4); } template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<'='<<h<<endl; } template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while(*sdbg!=',')cerr<<*sdbg++;cerr<<'='<<h<<','; _dbg(sdbg+1, a...); } template<class T> ostream& operator<<(ostream& os, vector<T> V) { os << "["; for (auto& vv : V) os << vv << ","; os << "]"; return os; } #ifdef LOCAL #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #else #define debug(...) (__VA_ARGS__) #define cerr if(0)cout #endif template<class C> ostream & operator<<(ostream &os, const set<C> &s){ os << "{"; for(auto a: s){ os << a << ", "; } os << "}"; return os; } struct Zelek{ int kolor, masa, cena; }; struct by_masa{ bool operator()(const Zelek &a, const Zelek &b){ return a.masa < b.masa; } }; struct by_cena{ bool operator()(const Zelek &a, const Zelek &b){ return a.cena < b.cena; } }; struct by_kolor{ bool operator()(const Zelek &a, const Zelek &b){ return a.kolor < b.kolor; } }; //Struct used for each Fibonacci heap node struct FibonacciNode { int degree; FibonacciNode* parent; FibonacciNode* child; FibonacciNode* left; FibonacciNode* right; bool mark; int key; int nodeIndex; }; int32_t main(){ ios_base::sync_with_stdio(false); cin.tie(0); int n,k,m; cin >> n >> k >> m; vector<Zelek> zelki(n); REP(i, n){ cin >> zelki[i].kolor >> zelki[i].masa >> zelki[i].cena; zelki[i].kolor--; } map<int, vector<Zelek>> kolor_to_zelki; for(auto z: zelki){ kolor_to_zelki[z.kolor].PB(z); } const int infty = 1e18; VI cost(m, infty); cost[0] = 0; for(int kolor = 0; kolor < k; kolor++){ VI new_cost(m, infty); for(auto z: kolor_to_zelki[kolor]){ for(int i = 0; i < m; i++){ int tm = i + z.masa; if(tm >= m){ tm -= m; } int tc = cost[i] + z.cena; if(tc < new_cost[tm]){ new_cost[tm] = tc; } } } cost = new_cost; debug(kolor, new_cost); } vector<bool> visited(m); VI end_cost(m, infty); end_cost[0] = 0; REP(i, m){ int min_cost = infty; int v = -1; REP(j, m){ if(!visited[j] && end_cost[j] < min_cost){ min_cost = end_cost[j]; v = j; } } if(v == -1){ break; } visited[v] = true; for(int j = 0; j < m; j++){ int tm = j + v; if(tm >= m){ tm -= m; } int tc = end_cost[v] + cost[j]; if(tc < end_cost[tm]){ end_cost[tm] = tc; } } } REP(i, m){ cout << (end_cost[i] == infty ? -1 : end_cost[i]) << "\n"; } }
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 137 138 139 140 141 142 143 144 145 146 | #include <bits/stdc++.h> using namespace std; #define int long long #define ALL(x) (x).begin(), (x).end() #define FOR(i, a, b) for(int i = a; i <= b; i++) #define FORD(i, a, b) for(int i = a; i >= b; i--) #define REP(i, n) for(int i = 0; i < n; i++) #define VI vector<int> #define PII pair<int,int> #define SZ(x) (int)((x).size()) #define PB push_back #define MP make_pair #define st first #define nd second template<class C> void mini(C& _a4, C _b4) { _a4 = min(_a4, _b4); } template<class C> void maxi(C& _a4, C _b4) { _a4 = max(_a4, _b4); } template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<'='<<h<<endl; } template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while(*sdbg!=',')cerr<<*sdbg++;cerr<<'='<<h<<','; _dbg(sdbg+1, a...); } template<class T> ostream& operator<<(ostream& os, vector<T> V) { os << "["; for (auto& vv : V) os << vv << ","; os << "]"; return os; } #ifdef LOCAL #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #else #define debug(...) (__VA_ARGS__) #define cerr if(0)cout #endif template<class C> ostream & operator<<(ostream &os, const set<C> &s){ os << "{"; for(auto a: s){ os << a << ", "; } os << "}"; return os; } struct Zelek{ int kolor, masa, cena; }; struct by_masa{ bool operator()(const Zelek &a, const Zelek &b){ return a.masa < b.masa; } }; struct by_cena{ bool operator()(const Zelek &a, const Zelek &b){ return a.cena < b.cena; } }; struct by_kolor{ bool operator()(const Zelek &a, const Zelek &b){ return a.kolor < b.kolor; } }; //Struct used for each Fibonacci heap node struct FibonacciNode { int degree; FibonacciNode* parent; FibonacciNode* child; FibonacciNode* left; FibonacciNode* right; bool mark; int key; int nodeIndex; }; int32_t main(){ ios_base::sync_with_stdio(false); cin.tie(0); int n,k,m; cin >> n >> k >> m; vector<Zelek> zelki(n); REP(i, n){ cin >> zelki[i].kolor >> zelki[i].masa >> zelki[i].cena; zelki[i].kolor--; } map<int, vector<Zelek>> kolor_to_zelki; for(auto z: zelki){ kolor_to_zelki[z.kolor].PB(z); } const int infty = 1e18; VI cost(m, infty); cost[0] = 0; for(int kolor = 0; kolor < k; kolor++){ VI new_cost(m, infty); for(auto z: kolor_to_zelki[kolor]){ for(int i = 0; i < m; i++){ int tm = i + z.masa; if(tm >= m){ tm -= m; } int tc = cost[i] + z.cena; if(tc < new_cost[tm]){ new_cost[tm] = tc; } } } cost = new_cost; debug(kolor, new_cost); } vector<bool> visited(m); VI end_cost(m, infty); end_cost[0] = 0; REP(i, m){ int min_cost = infty; int v = -1; REP(j, m){ if(!visited[j] && end_cost[j] < min_cost){ min_cost = end_cost[j]; v = j; } } if(v == -1){ break; } visited[v] = true; for(int j = 0; j < m; j++){ int tm = j + v; if(tm >= m){ tm -= m; } int tc = end_cost[v] + cost[j]; if(tc < end_cost[tm]){ end_cost[tm] = tc; } } } REP(i, m){ cout << (end_cost[i] == infty ? -1 : end_cost[i]) << "\n"; } } |