#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define ll long long
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, k, m;
cin >> n >> k >> m;
vector <vector <pair <ll, ll> > > v(k); // dla koloru {masa, cena}
for (int i = 0; i < n; i++) {
int a, b, c;
cin >> a >> b >> c;
a--;
v[a].push_back({b, c});
}
vector <ll> poj(m, LLONG_MAX);
poj[0] = 0;
for (int i = 0; i < k; i++) {
vector <ll> temp(m, LLONG_MAX);
for (auto [a, b] : v[i]) {
for (int j = 0; j < m; j++) {
if (poj[j] != LLONG_MAX) {
temp[(j+a)%m] = min(temp[(j+a)%m], poj[j]+b);
}
}
}
poj = temp;
}
// for (auto i : poj)
// cout << i << "\n";
vector <ll> dp(m, LLONG_MAX);
dp[0] = 0;
for (int i = 0; i < m; i++) {
if (poj[i] != LLONG_MAX) {
for (int j = 1; j < m; j++) {
dp[(i*j)%m] = min(dp[(i*j)%m], poj[i] * j);
}
}
}
// for (auto i : dp)
// cout << i << " ";
vector <bool> wag(m);
while (true) {
ll mincen = LLONG_MAX;
vector <int> wagi;
for (int i = 0; i < m; i++) {
if (wag[i])
continue;
if (mincen > dp[i]) {
mincen = dp[i];
wagi.clear();
wagi.push_back(i);
}
else if (mincen == dp[i]) {
wagi.push_back(i);
}
}
if (mincen == LLONG_MAX)
break;
for (auto w : wagi) {
wag[w] = 1;
for (int i = 0; i < m; i++) {
if (dp[i] != LLONG_MAX) {
dp[(i+w)%m] = min(dp[(i+w)%m], dp[i] + mincen);
}
}
}
}
for (auto i : dp) {
if (i == LLONG_MAX)
cout << "-1\n";
else
cout << 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 | #include <bits/stdc++.h> using namespace std; #define ff first #define ss second #define ll long long int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, k, m; cin >> n >> k >> m; vector <vector <pair <ll, ll> > > v(k); // dla koloru {masa, cena} for (int i = 0; i < n; i++) { int a, b, c; cin >> a >> b >> c; a--; v[a].push_back({b, c}); } vector <ll> poj(m, LLONG_MAX); poj[0] = 0; for (int i = 0; i < k; i++) { vector <ll> temp(m, LLONG_MAX); for (auto [a, b] : v[i]) { for (int j = 0; j < m; j++) { if (poj[j] != LLONG_MAX) { temp[(j+a)%m] = min(temp[(j+a)%m], poj[j]+b); } } } poj = temp; } // for (auto i : poj) // cout << i << "\n"; vector <ll> dp(m, LLONG_MAX); dp[0] = 0; for (int i = 0; i < m; i++) { if (poj[i] != LLONG_MAX) { for (int j = 1; j < m; j++) { dp[(i*j)%m] = min(dp[(i*j)%m], poj[i] * j); } } } // for (auto i : dp) // cout << i << " "; vector <bool> wag(m); while (true) { ll mincen = LLONG_MAX; vector <int> wagi; for (int i = 0; i < m; i++) { if (wag[i]) continue; if (mincen > dp[i]) { mincen = dp[i]; wagi.clear(); wagi.push_back(i); } else if (mincen == dp[i]) { wagi.push_back(i); } } if (mincen == LLONG_MAX) break; for (auto w : wagi) { wag[w] = 1; for (int i = 0; i < m; i++) { if (dp[i] != LLONG_MAX) { dp[(i+w)%m] = min(dp[(i+w)%m], dp[i] + mincen); } } } } for (auto i : dp) { if (i == LLONG_MAX) cout << "-1\n"; else cout << i << "\n"; } } |
English