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 f first
#define s second
using namespace std;

void Impossible(int m){
	cout << 0 << '\n';
	for(int i = 1; i < m; i++) cout << -1 << '\n';
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int n, k, m;
	cin >> n >> k >> m;
	vector<pair<int, long long>> zel[k];
	int a, b;
	long long c;
	for(int i = 0; i < n; i++){
		cin >> a >> b >> c;
		a--;
		zel[a].push_back({b, c});
	}
	long long pr[m], now[m];
	for(int i = 0; i < k; i++){
		if(zel[i].size() == 0){
			Impossible(m);
			return 0;
		}
	}
	for(int i = 0; i < m; i++) now[i] = 0;
	for(auto& i : zel[0]){
		if(!now[(i.f)%m]) now[(i.f)%m] = i.s;
		else now[(i.f)%m] = min(now[(i.f)%m], i.s);
	}
	for(int i = 1; i < k; i++){
		for(int j = 0; j < m; j++){
			pr[j] = now[j];
			now[j] = 0;
		}
		for(auto& j : zel[i]){
			for(int l = 0; l < m; l++){
				if(pr[l]){
					if(!now[(l + j.f)%m]) now[(l + j.f)%m] = pr[l] + j.s;
					else now[(l + j.f)%m] = min(now[(l + j.f)%m], pr[l] + j.s);
				}
			}
		}
	}
	long long wynik[m];
	wynik[0] = 0;
	for(int i = 1; i < m; i++) wynik[i] = -1;
	queue<int> q;
	q.push(0);
	int w;
	while(!q.empty()){
		w = q.front();
		q.pop();
		for(int i = 1; i < m; i++){
			if(now[i]){
				if(wynik[(w + i)%m] == -1 || wynik[(w + i)%m] > wynik[w] + now[i]){
					wynik[(w + i)%m] = wynik[w] + now[i];
					q.push((w + i)%m);
				}
			}
		}
	}
	for(int i = 0; i < m; i++) cout << wynik[i] << '\n';
	return 0;
}