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
83
84
85
86
87
88
89
90
91
92
#include <iostream>
#include <vector>
#include <queue>

struct Yelly {
    int mass;
    long long cost;

    Yelly(int mass, long long cost) : mass(mass), cost(cost) {}
};

int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int n, k, m;
    std::cin >> n >> k >> m;
    std::vector<std::vector<Yelly>> yellys(k + 1);
    for (int i = 0; i < n; ++i) {
        int color, mass;
        long long cost;
        std::cin >> color >> mass >> cost;
        yellys[color].emplace_back(mass, cost);
    }

    std::vector<long long> step_last(m, -1), step_curr(m, -1);
    step_last[0] = 0;
    bool missing = false;
    for (int i = 1; i <= k; ++i) {
        if (yellys[i].empty()) {
            missing = true;
            break;
        }

        step_curr.clear();
        step_curr.resize(m, -1);

        for (int j = 0; j < m; ++j) {
            if (step_last[j] == -1) continue;
            for (const auto& yelly : yellys[i]) {
                int mass = (j + yelly.mass) % m;
                long long cost = step_last[j] + yelly.cost;
                if (step_curr[mass] == -1 || cost < step_curr[mass]) {
                    step_curr[mass] = cost;
                }
            }
        }

        swap(step_curr, step_last);
    }

    if (missing) {
        std::cout << 0 << std::endl;
        for (int i = 1; i < m; ++i) {
            std::cout << -1 << std::endl;
        }
        return 0;
    }

    std::vector<long long> lowest_cost(m, -1);
    lowest_cost[0] = 0;
    std::priority_queue<std::pair<long long, int>> travel;
    std::vector<bool> processed(m, false);
    travel.emplace(0, 0);

    while (!travel.empty()) {
        auto [cost, mass] = travel.top();
        cost = -cost;
        travel.pop();
        if (processed[mass]) {
            continue;
        }

        processed[mass] = true;
        for (int i = 1; i < m; ++i) {
            if (step_last[i] == -1) continue;
            int new_mass = (mass + i) % m;
            long long new_cost = cost + step_last[i];
            if (!processed[new_mass] && (lowest_cost[new_mass] == -1 || new_cost < lowest_cost[new_mass])) {
                travel.emplace(-new_cost, new_mass);
                lowest_cost[new_mass] = new_cost;
            }
        }
    }

    for (int i = 0; i < m; ++i) {
        std::cout << lowest_cost[i] << std::endl;
    }

    return 0;
}