#include <bits/stdc++.h> #define pb push_back #define inf 99999999999999999LL #define int long long #define turbo ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); using namespace std; const int N = 7015; vector<pair<int, int>> zelki[N]; // Prefiks kolorów, reszta modulo m int dp[N][N]; int n, k, m; signed main() { turbo; cin >> n >> k >> m; for (int i = 0; i < n; i++) { int kk, mm, cc; cin >> kk >> mm >> cc; zelki[kk].pb({mm, cc}); } for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { dp[i][j] = inf; } } dp[0][0] = 0; for (int i = 1; i <= k; i++) { for (auto zelek : zelki[i]) { int mm = zelek.first; int cc = zelek.second; for (int j = 0; j < m; j++) { if (dp[i - 1][j] != inf) { dp[i][(j + mm) % m] = min(dp[i][(j + mm) % m], dp[i - 1][j] + cc); } } } } dp[k][0] = 0; vector <int> res(m); for (int i = 1; i < m; i++) { res[i] = inf; } set <pair <int, int>> s; for (int i = 1; i < m; i++) { for (int j = 0; j < m; j++) { if (res[j] != inf) { s.insert({j, res[j]}); } } while (!s.empty()) { auto it = s.begin(); int j = it->first; int c = it->second; s.erase(it); if (res[(j + i) % m] > c + dp[k][i]) { s.erase({(j + i) % m, res[(j + i) % m]}); res[(j + i) % m] = c + dp[k][i]; s.insert({(j + i) % m, res[(j + i) % m]}); } } } for (int i = 0; i < m; i++) { if (res[i] == inf) { cout << -1 << "\n"; } else { cout << res[i] << "\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 | #include <bits/stdc++.h> #define pb push_back #define inf 99999999999999999LL #define int long long #define turbo ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); using namespace std; const int N = 7015; vector<pair<int, int>> zelki[N]; // Prefiks kolorów, reszta modulo m int dp[N][N]; int n, k, m; signed main() { turbo; cin >> n >> k >> m; for (int i = 0; i < n; i++) { int kk, mm, cc; cin >> kk >> mm >> cc; zelki[kk].pb({mm, cc}); } for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { dp[i][j] = inf; } } dp[0][0] = 0; for (int i = 1; i <= k; i++) { for (auto zelek : zelki[i]) { int mm = zelek.first; int cc = zelek.second; for (int j = 0; j < m; j++) { if (dp[i - 1][j] != inf) { dp[i][(j + mm) % m] = min(dp[i][(j + mm) % m], dp[i - 1][j] + cc); } } } } dp[k][0] = 0; vector <int> res(m); for (int i = 1; i < m; i++) { res[i] = inf; } set <pair <int, int>> s; for (int i = 1; i < m; i++) { for (int j = 0; j < m; j++) { if (res[j] != inf) { s.insert({j, res[j]}); } } while (!s.empty()) { auto it = s.begin(); int j = it->first; int c = it->second; s.erase(it); if (res[(j + i) % m] > c + dp[k][i]) { s.erase({(j + i) % m, res[(j + i) % m]}); res[(j + i) % m] = c + dp[k][i]; s.insert({(j + i) % m, res[(j + i) % m]}); } } } for (int i = 0; i < m; i++) { if (res[i] == inf) { cout << -1 << "\n"; } else { cout << res[i] << "\n"; } } return 0; } |