#include <algorithm> #include <iostream> #include <queue> #include <vector> using namespace std; using LL = long long; using PII = pair<int, int>; using PLI = pair<LL, int>; using PLII = pair<LL, PII>; const LL INF = 1000000000000000000; bool missing_color(int k, const vector<vector<PII>>& z) { for (int i = 0; i < k; i++) { if (z[i].size() == 0) return true; } return false; } vector<LL> solve_missing_color(int m) { vector<LL> res(m, -1); res[0] = 0; return res; } vector<vector<PLI>> normalize(int k, int m, vector<vector<PII>>& z) { vector<vector<PLI>> t(k); for (int i = 0; i < k; ++i) { sort(z[i].begin(), z[i].end()); for (int j = 0; j < z[i].size(); ++j) { if (j == 0 || z[i][j].first != z[i][j - 1].first) t[i].push_back({ z[i][j].second, z[i][j].first }); } sort(t[i].begin(), t[i].end()); } sort(t.begin(), t.end(), [](const vector<PLI>& a, const vector<PLI>& b) { return a.size() < b.size(); }); int idx = 0; while (idx < t.size() - 1 && t[idx].size() == 1) { for (int i = 0; i < t[idx + 1].size(); ++i) { t[idx + 1][i].first += t[idx][0].first; t[idx + 1][i].second = (t[idx + 1][i].second + t[idx][0].second) % m; } ++idx; } vector<vector<PLI>> res(t.begin() + idx, t.end()); return res; } vector<LL> solve(int m, const vector<vector<PLI>>& z) { int k = z.size(); vector<vector<LL>> res(k, vector<LL>(m, INF)); res[0][0] = 0; priority_queue<PLII> q; q.push({ 0, { 0, 0 } }); while (!q.empty()) { LL ci = -q.top().first; int ki = q.top().second.first; int mi = q.top().second.second; q.pop(); if (ci != res[ki][mi]) continue; int nki = (ki + 1) % k; for (int i = 0; i < z[nki].size(); ++i) { LL nci = ci + z[nki][i].first; int nmi = (mi + z[nki][i].second) % m; if (nci < res[nki][nmi]) { res[nki][nmi] = nci; q.push({ -nci, { nki, nmi} }); } } } for (int i = 0; i < m; ++i) { if (res[0][i] == INF) res[0][i] = -1; } return res[0]; } int main() { int n, k, m, ki, mi, ci; cin >> n >> k >> m; vector<vector<PII>> z(k); for (int i = 0; i < n; ++i) { cin >> ki >> mi >> ci; z[ki % k].push_back({ mi % m, ci }); } vector<LL> res = missing_color(k, z) ? solve_missing_color(m) : solve(m, normalize(k, m, z)); for (int i = 0; i < m; ++i) { cout << res[i] << endl; } 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 129 130 131 132 133 134 135 136 137 | #include <algorithm> #include <iostream> #include <queue> #include <vector> using namespace std; using LL = long long; using PII = pair<int, int>; using PLI = pair<LL, int>; using PLII = pair<LL, PII>; const LL INF = 1000000000000000000; bool missing_color(int k, const vector<vector<PII>>& z) { for (int i = 0; i < k; i++) { if (z[i].size() == 0) return true; } return false; } vector<LL> solve_missing_color(int m) { vector<LL> res(m, -1); res[0] = 0; return res; } vector<vector<PLI>> normalize(int k, int m, vector<vector<PII>>& z) { vector<vector<PLI>> t(k); for (int i = 0; i < k; ++i) { sort(z[i].begin(), z[i].end()); for (int j = 0; j < z[i].size(); ++j) { if (j == 0 || z[i][j].first != z[i][j - 1].first) t[i].push_back({ z[i][j].second, z[i][j].first }); } sort(t[i].begin(), t[i].end()); } sort(t.begin(), t.end(), [](const vector<PLI>& a, const vector<PLI>& b) { return a.size() < b.size(); }); int idx = 0; while (idx < t.size() - 1 && t[idx].size() == 1) { for (int i = 0; i < t[idx + 1].size(); ++i) { t[idx + 1][i].first += t[idx][0].first; t[idx + 1][i].second = (t[idx + 1][i].second + t[idx][0].second) % m; } ++idx; } vector<vector<PLI>> res(t.begin() + idx, t.end()); return res; } vector<LL> solve(int m, const vector<vector<PLI>>& z) { int k = z.size(); vector<vector<LL>> res(k, vector<LL>(m, INF)); res[0][0] = 0; priority_queue<PLII> q; q.push({ 0, { 0, 0 } }); while (!q.empty()) { LL ci = -q.top().first; int ki = q.top().second.first; int mi = q.top().second.second; q.pop(); if (ci != res[ki][mi]) continue; int nki = (ki + 1) % k; for (int i = 0; i < z[nki].size(); ++i) { LL nci = ci + z[nki][i].first; int nmi = (mi + z[nki][i].second) % m; if (nci < res[nki][nmi]) { res[nki][nmi] = nci; q.push({ -nci, { nki, nmi} }); } } } for (int i = 0; i < m; ++i) { if (res[0][i] == INF) res[0][i] = -1; } return res[0]; } int main() { int n, k, m, ki, mi, ci; cin >> n >> k >> m; vector<vector<PII>> z(k); for (int i = 0; i < n; ++i) { cin >> ki >> mi >> ci; z[ki % k].push_back({ mi % m, ci }); } vector<LL> res = missing_color(k, z) ? solve_missing_color(m) : solve(m, normalize(k, m, z)); for (int i = 0; i < m; ++i) { cout << res[i] << endl; } return 0; } |