#include<bits/stdc++.h> using namespace std; typedef long long ll; const int N = 2e2 + 10; const int P = 101; const ll MLL = 1e18; const ll LOG = 31; // robimy lewa i prawa a potem rekursywnie do przedialu na dole void solve() { int n , kol, mod; cin >> n >> kol >> mod; vector<int> cnt(kol + 1); vector<vector<pair<int , ll>>> colors(kol + 1); for(int i = 0; i < n; i++) { int k , m; ll c; cin >> k >> m >> c; cnt[k]++; colors[k].push_back({m, c}); } bool ok = true; for(int i = 1; i <= kol;i++) { if(cnt[i] == 0) ok = false; } cout << 0 << endl; if(!ok) { for(int i = 1; i < mod; i++) cout << -1 << endl; return; } vector<ll> dp(mod, MLL); dp[0] = 0; // obliczamy sume k dla kazdego modulo for(int i = 1; i <= kol; i++) { // O(k * m + n * m) vector<ll> add(mod, MLL); for(pair<int , ll> x : colors[i]) { for(int j = 0; j < mod; j++) { if(dp[j] != MLL) { add[(j + x.first) % mod] = min(add[(j + x.first) % mod] , dp[j] + x.second); } } } for(int j = 0; j < mod; j++) { dp[j] = add[j]; } } vector<pair<int , ll>> edges; set<pair<ll, int>> s; for(int i = 0; i < mod; i++) { if(dp[i] != MLL) { edges.push_back({i , dp[i]}); s.insert({dp[i], i}); } } while(s.size() > 0) { auto it = *(s.begin()); s.erase(s.find(it)); ll val = it.first; int pos = it.second; for(pair<int , ll> x : edges) { ll ne = val + x.second; int ind = (pos + x.first) % mod; // patrzymy czy mamy lepsza oferte if(dp[ind] > ne) { if(s.find({dp[ind], ind}) != s.end()) { s.erase(s.find({dp[ind], ind})); } s.insert({ne , ind}); dp[ind] = ne; } } } for(int i = 1; i < mod; i++) { if(dp[i] == MLL) cout << -1; else cout << dp[i]; cout << endl; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); solve(); 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 | #include<bits/stdc++.h> using namespace std; typedef long long ll; const int N = 2e2 + 10; const int P = 101; const ll MLL = 1e18; const ll LOG = 31; // robimy lewa i prawa a potem rekursywnie do przedialu na dole void solve() { int n , kol, mod; cin >> n >> kol >> mod; vector<int> cnt(kol + 1); vector<vector<pair<int , ll>>> colors(kol + 1); for(int i = 0; i < n; i++) { int k , m; ll c; cin >> k >> m >> c; cnt[k]++; colors[k].push_back({m, c}); } bool ok = true; for(int i = 1; i <= kol;i++) { if(cnt[i] == 0) ok = false; } cout << 0 << endl; if(!ok) { for(int i = 1; i < mod; i++) cout << -1 << endl; return; } vector<ll> dp(mod, MLL); dp[0] = 0; // obliczamy sume k dla kazdego modulo for(int i = 1; i <= kol; i++) { // O(k * m + n * m) vector<ll> add(mod, MLL); for(pair<int , ll> x : colors[i]) { for(int j = 0; j < mod; j++) { if(dp[j] != MLL) { add[(j + x.first) % mod] = min(add[(j + x.first) % mod] , dp[j] + x.second); } } } for(int j = 0; j < mod; j++) { dp[j] = add[j]; } } vector<pair<int , ll>> edges; set<pair<ll, int>> s; for(int i = 0; i < mod; i++) { if(dp[i] != MLL) { edges.push_back({i , dp[i]}); s.insert({dp[i], i}); } } while(s.size() > 0) { auto it = *(s.begin()); s.erase(s.find(it)); ll val = it.first; int pos = it.second; for(pair<int , ll> x : edges) { ll ne = val + x.second; int ind = (pos + x.first) % mod; // patrzymy czy mamy lepsza oferte if(dp[ind] > ne) { if(s.find({dp[ind], ind}) != s.end()) { s.erase(s.find({dp[ind], ind})); } s.insert({ne , ind}); dp[ind] = ne; } } } for(int i = 1; i < mod; i++) { if(dp[i] == MLL) cout << -1; else cout << dp[i]; cout << endl; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); solve(); return 0; } |