#include <bits/stdc++.h>
#define pb push_back
using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pii;
const long long MAX = 1e18;
const int N = 7003;
vector<pair<int, long long>> reszty[N];
vector<long long> poj[2];
vector<long long> optymalne;
vector<long long> ostateczne;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, k, m;
cin >> n >> k >> m;
for(int i = 0; i < N; i++) {
poj[0].pb(MAX);
poj[1].pb(MAX);
optymalne.pb(MAX);
ostateczne.pb(MAX);
}
for(int i = 0; i < n; i++) {
int ki, mi, ci;
cin >> ki >> mi >> ci;
reszty[ki].pb({mi%m, ci});
}
poj[0][0] = 0;
for(int t = 1; t <= k; t++) {
for(int r = 0; r < m; r++) {
int ile = reszty[t].size();
for(int i = 0; i < ile; i++) {
if(t % 2 == 0) {
poj[0][r] = min(poj[0][r], poj[1][(r-reszty[t][i].first+m)%m] + reszty[t][i].second);
}
else poj[1][r] = min(poj[1][r], poj[0][(r-reszty[t][i].first+m)%m] + reszty[t][i].second);
}
}
for(int i = 0; i < m; i++) poj[(t+1)%2][i] = MAX;
}
int b = k % 2;
for(int r = 0; r < m; r++) {
if(poj[b][r] != MAX) {
for(int i = 0; i < m; i++) {
optymalne[(i*r)%m] = min(optymalne[(i*r)%m], i * poj[b][r]);
}
}
}
ostateczne[0] = 0;
for(int i = 0; i < m; i++) {
for(int r = 1; r < m; r++) {
ostateczne[r] = min(ostateczne[r], ostateczne[(r-i+m)%m] + optymalne[i]);
}
}
for(int r = 0; r < m; r++) {
if(ostateczne[r] == MAX) cout << -1 << "\n";
else cout << ostateczne[r] << "\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 75 76 77 78 79 80 81 82 | #include <bits/stdc++.h> #define pb push_back using namespace std; typedef vector<int> vi; typedef pair<int, int> pii; const long long MAX = 1e18; const int N = 7003; vector<pair<int, long long>> reszty[N]; vector<long long> poj[2]; vector<long long> optymalne; vector<long long> ostateczne; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, k, m; cin >> n >> k >> m; for(int i = 0; i < N; i++) { poj[0].pb(MAX); poj[1].pb(MAX); optymalne.pb(MAX); ostateczne.pb(MAX); } for(int i = 0; i < n; i++) { int ki, mi, ci; cin >> ki >> mi >> ci; reszty[ki].pb({mi%m, ci}); } poj[0][0] = 0; for(int t = 1; t <= k; t++) { for(int r = 0; r < m; r++) { int ile = reszty[t].size(); for(int i = 0; i < ile; i++) { if(t % 2 == 0) { poj[0][r] = min(poj[0][r], poj[1][(r-reszty[t][i].first+m)%m] + reszty[t][i].second); } else poj[1][r] = min(poj[1][r], poj[0][(r-reszty[t][i].first+m)%m] + reszty[t][i].second); } } for(int i = 0; i < m; i++) poj[(t+1)%2][i] = MAX; } int b = k % 2; for(int r = 0; r < m; r++) { if(poj[b][r] != MAX) { for(int i = 0; i < m; i++) { optymalne[(i*r)%m] = min(optymalne[(i*r)%m], i * poj[b][r]); } } } ostateczne[0] = 0; for(int i = 0; i < m; i++) { for(int r = 1; r < m; r++) { ostateczne[r] = min(ostateczne[r], ostateczne[(r-i+m)%m] + optymalne[i]); } } for(int r = 0; r < m; r++) { if(ostateczne[r] == MAX) cout << -1 << "\n"; else cout << ostateczne[r] << "\n"; } return 0; } |
English