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
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
#include<cstdio>
#include<vector>

using namespace std;

void clearMacierz(int m, vector<long long>& macierz2)
{
    for (int j = 0; j < m; j++) {
        macierz2[j] = -1;
    }
}

int main() {
    bool debug = false;

    int n, k, m;

    scanf("%d %d %d", &n, &k, &m);

    vector<long long int> result(m, -1);
    

    vector<vector<pair<int, long long>>> zelki(k + 1);

    for (int i = 0; i < n; i++) {
        int ki, mi;
        long long ci;

        scanf("%d %d %lld", &ki, &mi, &ci);

        zelki[ki].push_back(make_pair(mi, ci));
    }

    bool found = true;

    vector<long long> macierz1(m, -1);
    vector<long long> macierz2(m, -1);

    macierz2[0] = 0;

    for (int i = 1; i <= k; i++) {
        if (zelki[i].empty()) {
            found = false;
            break;
        }

        swap(macierz1, macierz2);
        clearMacierz(m, macierz2);

        for (auto it = zelki[i].begin(); it != zelki[i].end(); ++it) {
            for (int j = 0; j < m; j++) {
                if (macierz1[j] != -1) {
                    if (macierz2[(j + it->first) % m] == -1) {
                        macierz2[(j + it->first) % m] = macierz1[j] + it->second;
                    }
                    else {
                        macierz2[(j + it->first) % m] = min(macierz1[j] + it->second, macierz2[(j + it->first) % m]);
                    }
                }
            }
        }
    }

    if (found) {
        vector<pair<int, long long>> przejscie;

        for (int j = 0; j < m; j++) {
            if (macierz2[j] != -1) {
                przejscie.push_back(make_pair(j, macierz2[j]));
                result[j] = macierz2[j];

                if (debug) printf("%d %lld\n", j, macierz2[j]);
            }
        }

        while (found) {
            found = false;
            swap(macierz1, macierz2);
            clearMacierz(m, macierz2);

            for (auto it = przejscie.begin(); it != przejscie.end(); ++it) {
                for (int j = 0; j < m; j++) {
                    if (macierz1[j] != -1) {
                        if (macierz2[(j + it->first) % m] == -1) {
                            macierz2[(j + it->first) % m] = macierz1[j] + it->second;
                        }
                        else {
                            macierz2[(j + it->first) % m] = min(macierz1[j] + it->second, macierz2[(j + it->first) % m]);
                        }
                        if (result[(j + it->first) % m] == -1 || result[(j + it->first) % m] > macierz2[(j + it->first) % m]) {
                            found = true;
                            result[(j + it->first) % m] = macierz2[(j + it->first) % m];
                            if (result[0] != 0) {
                                if (debug) printf("xxx?\n");
                            }
                        }
                    }
                }
            }
        }
    }

    result[0] = 0;
    for (auto it = result.begin(); it != result.end(); ++it) {
        printf("%lld\n", *it);
    }

    return 0;
}