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;
struct trojka{
    long long kolor;
    long long waga;
    long long cena;
};
bool comp_tr(trojka a,trojka b){
    return a.kolor < b.kolor;
}
bool czy_zrobione[7001];
long long arr_new[7001],arr_old[7001];
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    long long n,k,m;
    cin >> n >> k >> m;
    vector <trojka> Input;
    Input.push_back({-1,-1,-1});
    for (long long i = 1; i <= n;i++){
        long long a,b,c;
        cin >> a >> b >> c;
        Input.push_back({a,b,c});
    }
    sort(Input.begin(),Input.end(),comp_tr);
    for (long long i = 0; i < m;i++){
        arr_new[i] = -1;
        arr_old[i] = -1;
    }
    arr_old[0] = 0;
    long long licz = 1;
    for (long long i = 1; i <= k;i++){
        bool did_we_do = 0;
        while(licz <= n && Input[licz].kolor == i){
            did_we_do = 1;
            for (long long j = 0; j < m;j++){
                if(arr_old[j] != -1){
                    arr_new[(j + Input[licz].waga) % m] = min(arr_new[(j + Input[licz].waga) % m],arr_old[j] + Input[licz].cena);
                    if(arr_new[(j + Input[licz].waga) % m] == -1){
                        arr_new[(j + Input[licz].waga) % m] = arr_old[j] + Input[licz].cena;
                    }
                }
            }
            licz++;
        }
        for (long long j = 0; j < m;j++){
            arr_old[j] = arr_new[j];
            arr_new[j] = -1;
        }
    }
    arr_old[0] = 0;
    for (long long i = 0; i < m;i++)czy_zrobione[i] = 0;
    priority_queue < pair <long long,long long> > Q;
    for (long long i = 0; i < m;i++){
        if(arr_old[i] != -1){
            Q.push({-arr_old[i],i});
        }
    }
    while(Q.size() > 0){
        long long war = -Q.top().first;
        long long ind = Q.top().second;
        Q.pop();
        if(czy_zrobione[ind] == 0){
            czy_zrobione[ind] = 1;
            arr_old[ind] = war;
            for (long long j = 0; j < m;j++){
                if(arr_old[j] != -1 && czy_zrobione[(ind + j)%m] == 0){
                    if(arr_old[(ind+j)%m] == -1 || arr_old[(ind+j)%m] > arr_old[j] + war){
                        Q.push({-arr_old[j] - war,(ind + j)%m});
                        arr_old[(ind+j)%m] = arr_old[j] + war;
                    }
                }
            }
        }
    }
    for (long long i = 0; i < m;i++){
         cout << arr_old[i]  << '\n';
    }
}