#include <bits/stdc++.h> using namespace std; typedef long long ll; struct zelek { int masa; ll cena; zelek (int m, ll c) { masa = m; cena = c; } bool operator<(const zelek &z) const { return cena > z.cena; } }; const int N = 7e3 + 5; const ll INF = 1e17 + 5; ll dp[N][N]; // k zelkow do tej pory, modulo ll minim[N]; bool Empty(vector <zelek> &v, int &iv) { return iv >= v.size(); } void iterate(vector <zelek> &v, int &iv, zelek &x, int &m, vector <zelek> &drugi){ int i = iv; while(i < v.size()){ zelek y = v[i]; int mi = (x.masa + y.masa) % m; ll ci = x.cena + y.cena; if(ci < minim[mi]) { minim[mi] = ci; drugi.push_back(zelek(mi, ci)); } i++; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, k, m; cin >> n >> k >> m; vector <vector <zelek> > kolor(k + 1); for(int i = 1; i <= n; i++) { int ki, mi; ll ci; cin >> ki >> mi >> ci; kolor[ki].push_back(zelek(mi % m, ci)); } for(int i = 1; i < m; i++) dp[0][i] = INF; for(int i = 1; i <= k; i++) { if(kolor[i].empty()) { cout << "0\n"; for(int j = 1; j < m; j++) cout << "-1\n"; return 0; } for(int j = 0; j < m; j++) dp[i][j] = INF; for(zelek x : kolor[i]) for(int j = 0; j < m; j++) dp[i][(j + x.masa) % m] = min(dp[i][(j + x.masa) % m], dp[i - 1][j] + x.cena); } priority_queue <zelek> tmp_q; for(int i = 1; i < m; i++) { minim[i] = dp[k][i]; if(minim[i] != INF) tmp_q.push(zelek(i, minim[i])); } vector <zelek> v1; vector <zelek> v2; int iv1 = 0, iv2 = 0; while(!tmp_q.empty()) { v1.push_back(tmp_q.top()); tmp_q.pop(); } while(!Empty(v1, iv1) || !Empty(v2, iv2)) { bool z_ktorej_tablicy = 0; zelek x = zelek(0, 0); if(Empty(v1, iv1) || (!Empty(v2, iv2) && v2[iv2].cena < v1[iv1].cena)) { z_ktorej_tablicy = 1; x = v2[iv2]; } else x = v1[iv1]; iterate(v1, iv1, x, m, v2); iterate(v2, iv2, x, m, v2); if(z_ktorej_tablicy) iv2++; else iv1++; } for(int i = 0; i < m; i++) if(minim[i] != INF) cout << minim[i] << "\n"; else cout << "-1\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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; struct zelek { int masa; ll cena; zelek (int m, ll c) { masa = m; cena = c; } bool operator<(const zelek &z) const { return cena > z.cena; } }; const int N = 7e3 + 5; const ll INF = 1e17 + 5; ll dp[N][N]; // k zelkow do tej pory, modulo ll minim[N]; bool Empty(vector <zelek> &v, int &iv) { return iv >= v.size(); } void iterate(vector <zelek> &v, int &iv, zelek &x, int &m, vector <zelek> &drugi){ int i = iv; while(i < v.size()){ zelek y = v[i]; int mi = (x.masa + y.masa) % m; ll ci = x.cena + y.cena; if(ci < minim[mi]) { minim[mi] = ci; drugi.push_back(zelek(mi, ci)); } i++; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, k, m; cin >> n >> k >> m; vector <vector <zelek> > kolor(k + 1); for(int i = 1; i <= n; i++) { int ki, mi; ll ci; cin >> ki >> mi >> ci; kolor[ki].push_back(zelek(mi % m, ci)); } for(int i = 1; i < m; i++) dp[0][i] = INF; for(int i = 1; i <= k; i++) { if(kolor[i].empty()) { cout << "0\n"; for(int j = 1; j < m; j++) cout << "-1\n"; return 0; } for(int j = 0; j < m; j++) dp[i][j] = INF; for(zelek x : kolor[i]) for(int j = 0; j < m; j++) dp[i][(j + x.masa) % m] = min(dp[i][(j + x.masa) % m], dp[i - 1][j] + x.cena); } priority_queue <zelek> tmp_q; for(int i = 1; i < m; i++) { minim[i] = dp[k][i]; if(minim[i] != INF) tmp_q.push(zelek(i, minim[i])); } vector <zelek> v1; vector <zelek> v2; int iv1 = 0, iv2 = 0; while(!tmp_q.empty()) { v1.push_back(tmp_q.top()); tmp_q.pop(); } while(!Empty(v1, iv1) || !Empty(v2, iv2)) { bool z_ktorej_tablicy = 0; zelek x = zelek(0, 0); if(Empty(v1, iv1) || (!Empty(v2, iv2) && v2[iv2].cena < v1[iv1].cena)) { z_ktorej_tablicy = 1; x = v2[iv2]; } else x = v1[iv1]; iterate(v1, iv1, x, m, v2); iterate(v2, iv2, x, m, v2); if(z_ktorej_tablicy) iv2++; else iv1++; } for(int i = 0; i < m; i++) if(minim[i] != INF) cout << minim[i] << "\n"; else cout << "-1\n"; } |