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
#include <bits/stdc++.h>
#define ll long long
using namespace std;

ll argmin(vector<ll>& answer){
    ll x = LONG_LONG_MAX, in2dex = 0;
    for(ll i=0; i<answer.size(); i++) if(answer[i] < x){
        x = answer[i];
        in2dex = i;
    }
    return in2dex;
}

int main(){
    ios_base::sync_with_stdio(0);
    cout.tie(0); cin.tie(0);
    ll n, col, m; cin>>n>>col>>m;
    //---------------------
    vector<vector<pair<ll, ll>>> data(col);
    for(ll i=0; i<n; i++){
        ll colour, weight, cost; cin>>colour>>weight>>cost;
        data[colour-1].push_back({weight, cost});
    }
    //---------------------
    for(auto k: data){
        if(k.size() == 0){
            cout<<"0\n";
            m--; while(m--){
                cout<<"-1\n";
            }
            return 0;
        }
    }
    //---------------------
    vector<ll> answer(m, LONG_LONG_MAX), result(m, LONG_LONG_MAX), new_result(m, LONG_LONG_MAX); 

    for(auto k: data[0]) result[k.first % m] = min(result[k.first % m], k.second);

    for(ll i=1; i<col; i++){
        new_result.assign(m, LONG_LONG_MAX);
        for(auto k: data[i]){
            for(ll j=0; j<m; j++){
                if(result[j] == LONG_LONG_MAX) continue;
                new_result[(k.first + j) % m] = min(new_result[(k.first + j) % m], result[j] + k.second);
            }
        }
        swap(result, new_result);
    }

    for(ll i=0; i<m; i++) answer[i] = min(answer[i], result[i]);

    ll opennodes = 0, donenodes = 0;
    for(auto k: answer) if(k != LONG_LONG_MAX) opennodes++;
    vector<ll> d(m, LONG_LONG_MAX);
    while(true){
        ll v = argmin(answer); if(answer[v] == LONG_LONG_MAX) break;

        d[v] = answer[v]; donenodes++;
        for(ll i=0; i<m; i++){
            if(d[(v + i) % m] == LONG_LONG_MAX && d[i] != LONG_LONG_MAX){
                if(answer[(v + i) % m] == LONG_LONG_MAX) opennodes++;
                answer[(v + i) % m] = min(answer[(v + i) % m], d[v] + d[i]);
            }
        }
        answer[v] = LONG_LONG_MAX;
    }
    //---------------------
    d[0] = 0;
    for(auto k: d) if(k != LONG_LONG_MAX) cout<<k<<'\n'; else cout<<"-1\n";
    return 0;
}