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
//ANMHLIJKTJIY!
#pragma GCC optimize(2)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline","fast-math","unroll-loops","no-stack-protector")
#pragma GCC diagnostic error "-fwhole-program"
#pragma GCC diagnostic error "-fcse-skip-blocks"
#pragma GCC diagnostic error "-funsafe-loop-optimizations"
#include <bits/stdc++.h>
#define INF 1000000000
#define LINF 1000000000000000000
#define MOD 1000000007
#define mod 998244353
#define F first
#define S second
#define ll long long
#define N 7010
using namespace std;
ll n,m,k,cost[N],f[N];
bool vis[N];
vector<pair<ll,ll> > nds[N];
int main(){
	ll i,j,x,y,w;
	scanf("%lld%lld%lld",&n,&k,&m);
	for(i=0;i<n;i++)
	{
		scanf("%lld%lld%lld",&x,&y,&w);
		x--;
		nds[x].push_back(make_pair(y%m,w));
	}
	memset(cost,63,sizeof(cost));
	cost[0]=0;
	for(i=0;i<k;i++)
	{
		if(nds[i].empty())
		{
			puts("0");
			for(i=1;i<m;i++)
			{
				puts("-1");
			}
			return 0;
		}
		memset(f,63,sizeof(f));
		for(x=0;x<m;x++)
		{
			if(cost[x]<LINF)
			{
				for(j=0;j<nds[i].size();j++)
				{
					f[(x+nds[i][j].F)%m]=min(f[(x+nds[i][j].F)%m],cost[x]+nds[i][j].S);
				}
			}
		}
		for(x=0;x<m;x++)
		{
			cost[x]=f[x];
		}
	}
	memset(f,63,sizeof(f));
	f[0]=0;
	while(true)
	{
		ll mn=-1;
		for(i=0;i<m;i++)
		{
			if(!vis[i])
			{
				if(mn==-1||f[mn]>f[i])
				{
					mn=i;
				}
			}
		}
		if(mn==-1)
		{
			break;
		}
		x=mn;
		vis[x]=true;
		for(i=0;i<m;i++)
		{
			f[(x+i)%m]=min(f[(x+i)%m],f[x]+cost[i]);
		}
	}
	for(i=0;i<m;i++)
	{
		if(f[i]>LINF)
		{
			f[i]=-1;
		}
		printf("%lld\n",f[i]);
	}
	return 0;
}