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
#include <iostream>
#include <algorithm>
#include <vector>
#define MAXN 7002
#define LL long long
#define PLI pair<LL, int>
#define INF 1000000000000000000ll

using namespace std;

int n, k, m;
vector<pair<int, int>> v[MAXN];
LL cost[MAXN][MAXN]; // [next col, mass % m]

vector<int> ncc[MAXN];

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    
    cin >> n >> k >> m;
    for (int i = 0; i < n; i++) {
        int a, b, c;
        cin >> a >> b >> c; // k, m, c
        if (b == m) b = 0;
        v[a - 1].push_back({b, c});
    }

    for (int i = 0; i < k; i++) {
        ncc[i].reserve(v[i].size());
        for (int j = 0; j < m; j++) {
            cost[i][j] = INF;
        }
    }
    
    cost[0][0] = 0;
    ncc[0] = {0};
    int k1 = 0;
    while (ncc[k1].size()) {
        int k2 = (k1 + 1) % k;
        ncc[k2].clear();
        for (auto m1 : ncc[k1]) {
            for (auto el : v[k1]) {
                int m2 = m1 + el.first;
                if (m2 >= m) m2 -= m;
                LL nc = cost[k1][m1] + el.second;
                if (nc < cost[k2][m2]) {
                    cost[k2][m2] = nc;
                    ncc[k2].push_back(m2);
                }
            }
        }
        k1 = k2;
    }

    for (int i = 0; i < m; i++) {
        if (cost[0][i] == INF) 
            cout << "-1\n";
        else
            cout << cost[0][i] << "\n";
    }
    return 0;
}