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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstdlib>

int c;

#define SKIP_WHITESPACE \
{   \
    while (1) { \
        c = fgetc(stdin);   \
        if (c != ' ' && c != '\n' && c != '\r') \
            break;  \
    }   \
}   \

#define READ_INT    \
({   \
    SKIP_WHITESPACE \
    int ret = c - '0';  \
    while (1) { \
        c = fgetc(stdin);   \
        if (c < '0' || c > '9') \
            break;  \
        ret = ret * 10 + c - '0';   \
    }   \
    ret; \
})   \


#define INF (uint64_t(-1))

struct zel {
    uint16_t k;
    uint16_t m;
    uint32_t c;

    bool operator<(const struct zel& par) {
        if (k != par.k)
            return k < par.k;
        if (m != par.m)
            return m > par.m;
        return c < par.c;
    }
} zels[7000];

struct kol {
    short start;
    short end;
} kols[7000];

uint64_t best[7000];
uint64_t tab[2][7000];

int active[2][7000];

int main(int argc, char* argv[]) {
    std::ios_base::sync_with_stdio (false);

    int n, k, m;
    n = READ_INT;
    k = READ_INT;
    m = READ_INT;

    int i, j;
    for (i = 0; i < n; ++i) {
        zels[i].k = READ_INT - 1;
        zels[i].m = READ_INT;
        zels[i].c = READ_INT;
    }
    std::sort(zels, zels + n);

    int ck = -1;
    for (i = 0; i < n; ++i) {
        if (zels[i].k == ck)
            continue;
        if (ck != -1) {
            kols[ck].end = i;
        }
        ck = zels[i].k;
        kols[ck].start = i;
    }
    kols[ck].end = i;

    for (i = 0; i < k; ++i) {
        if (kols[i].start == kols[i].end) {
            std::cout << "0\n";
            for (j = 1; j < m; ++j) {
                std::cout << "-1\n";
            }
            return EXIT_SUCCESS;
        }
    }

    for (i = 1; i < m; ++i) {
        best[i] = INF;
    }

    int nt = 0, pt = 1;
    int ac[2];
    tab[pt][0] = 0;
    tab[nt][0] = INF;
    for (i = 1; i < m; ++i) {
        tab[pt][i] = INF;
        tab[nt][i] = INF;
    }
    active[pt][0] = 0;
    ac[pt] = 1;
    ac[nt] = 0;

    int r, nj, count = 1, g;
    uint64_t nc;
    int z = 0;
    while (count > 0) {
        for (i = 0; i < k; ++i) {
            for (j = 0; j < ac[nt]; ++j) {
                tab[nt][active[nt][j]] = INF;
            }
            ac[nt] = 0;
            for (g = 0; g < ac[pt]; ++g) {
                j = active[pt][g];
                if (tab[pt][j] == INF)
                    continue;
                for (r = kols[i].start; r < kols[i].end; ++r) {
                    nj = (j + zels[r].m) % m;
                    nc = tab[pt][j] + zels[r].c;
                    if (nc < tab[nt][nj]) {
                        if (tab[nt][nj] == INF) {
                            active[nt][ac[nt]++] = nj;
                        }
                        tab[nt][nj] = nc;
                    }
                }
            }
            pt = (pt + 1) % 2;
            nt = (nt + 1) % 2;
        }

        count = 0;
        for (g = 0; g < ac[pt]; ++g) {
            i = active[pt][g];
            if (tab[pt][i] < best[i]) {
                best[i] = tab[pt][i];
                count++;
            } else {
                tab[pt][i] = INF;
            }
        }
    }
    for (i = 0; i < m; ++i) {
        if (best[i] == INF)
            std::cout << "-1\n";
        else
            std::cout << best[i] << "\n";
    }

    return EXIT_SUCCESS;
}