#include<bits/stdc++.h> #define MAXN 7011 #define INF 1000000000000000311 #define x first #define y second using namespace std; int n, k, m; int ki, mi, ci; vector<pair<int, int> > items[MAXN]; long long tab[MAXN][MAXN]; int main() { scanf("%d%d%d", &n, &k, &m); for (int i = 0; i < n; i++) { scanf("%d%d%d", &ki, &mi, &ci); items[ki].push_back({mi, ci}); } for (int i = 1; i <= k; i++) { if (items[i].size() == 0) { printf("0\n"); for (int j = 1; j < m; j++) { printf("-1\n"); } return 0; } } for (int i = 0; i <= k; i++) { for (int j = 0; j < m; j++) { tab[i][j] = INF; } } tab[0][0] = 0; tab[k][0] = 0; for (int i = 1; i <= k; i++) { for (int ii = 0; ii < items[i].size(); ii++) { for (int j = 0; j < m; j++) { tab[i][(j + items[i][ii].x) % m] = min(tab[i][(j + items[i][ii].x) % m], tab[i - 1][j] + items[i][ii].y); } } } for (int i = 1; i < m; i++) { for (int j = 1; j < m; j++) { int idx = j; while (tab[k][(i + idx) % m] > tab[k][i] + tab[k][idx]) { tab[k][(i + idx) % m] = tab[k][i] + tab[k][idx]; idx = (i + idx) % m; } } } for (int i = 0; i < m; i++) { if (tab[k][i] > 1000000000000000000) { printf("-1\n"); } else { printf("%lld\n", tab[k][i]); } } 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 | #include<bits/stdc++.h> #define MAXN 7011 #define INF 1000000000000000311 #define x first #define y second using namespace std; int n, k, m; int ki, mi, ci; vector<pair<int, int> > items[MAXN]; long long tab[MAXN][MAXN]; int main() { scanf("%d%d%d", &n, &k, &m); for (int i = 0; i < n; i++) { scanf("%d%d%d", &ki, &mi, &ci); items[ki].push_back({mi, ci}); } for (int i = 1; i <= k; i++) { if (items[i].size() == 0) { printf("0\n"); for (int j = 1; j < m; j++) { printf("-1\n"); } return 0; } } for (int i = 0; i <= k; i++) { for (int j = 0; j < m; j++) { tab[i][j] = INF; } } tab[0][0] = 0; tab[k][0] = 0; for (int i = 1; i <= k; i++) { for (int ii = 0; ii < items[i].size(); ii++) { for (int j = 0; j < m; j++) { tab[i][(j + items[i][ii].x) % m] = min(tab[i][(j + items[i][ii].x) % m], tab[i - 1][j] + items[i][ii].y); } } } for (int i = 1; i < m; i++) { for (int j = 1; j < m; j++) { int idx = j; while (tab[k][(i + idx) % m] > tab[k][i] + tab[k][idx]) { tab[k][(i + idx) % m] = tab[k][i] + tab[k][idx]; idx = (i + idx) % m; } } } for (int i = 0; i < m; i++) { if (tab[k][i] > 1000000000000000000) { printf("-1\n"); } else { printf("%lld\n", tab[k][i]); } } return 0; } |