#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define i128 __int128
#define mem(x) memset(x,0,sizeof(x))
#define endl "\n"
#define printYes cout << "Yes\n"
#define printYES cout << "YES\n"
#define printNo cout << "No\n"
#define printNO cout << "NO\n"
#define lowbit(x) ((x)&(-(x)))
#define pb push_back
#define mkp make_pair
#define pii pair<int,int>
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define rep(i,j,k) for (int i=(j);i<=(k);i++)
#define per(i,j,k) for (int i=(j);i>=(k);i--)
#define pcnt(x) __builtin_popcount(x)
mt19937 rnd(time(0));
template<class T>void chkmin(T&x,T y){x=min(x,y);}
template<class T>void chkmax(T&x,T y){x=max(x,y);}
const ll inf=1000000000000000000;
//const ll inf=1000000000;
//const ll mod=998244353;
//const ll mod=1000000007;
const int N=7005;
int n,k,m;
ll dp[N],g[N],ans[N];
vector<pii>t[N];
bool vis[N];
queue<int>q;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin >> n >> k >> m;
rep(i,0,m-1) dp[i]=inf,ans[i]=inf;
dp[0]=0;
rep(i,1,n)
{
int x,y,z;
cin >> x >> y >> z;
t[x].pb(mkp(y,z));
}
rep(i,1,k)
{
rep(j,0,m-1) g[j]=dp[j],dp[j]=inf;
rep(j,0,m-1)
{
for (pii o:t[i])
{
chkmin(dp[(j+o.fi)%m],g[j]+o.se);
}
}
}
ans[0]=0;
vis[0]=1;
q.push(0);
while (!q.empty())
{
int u=q.front();
q.pop();
rep(i,0,m-1) chkmin(ans[(u+i)%m],ans[u]+dp[i]);
int v=-1;
rep(i,0,m-1)
{
if (vis[i]) continue;
if (v==-1||ans[i]<ans[v]) v=i;
}
if (v==-1) break;
q.push(v);
vis[v]=1;
}
rep(i,0,m-1)
{
if (ans[i]==inf) cout << "-1\n";
else cout << ans[i] << "\n";
}
return 0;
}
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 | #include <bits/stdc++.h> using namespace std; #define ll long long #define ull unsigned long long #define i128 __int128 #define mem(x) memset(x,0,sizeof(x)) #define endl "\n" #define printYes cout << "Yes\n" #define printYES cout << "YES\n" #define printNo cout << "No\n" #define printNO cout << "NO\n" #define lowbit(x) ((x)&(-(x))) #define pb push_back #define mkp make_pair #define pii pair<int,int> #define fi first #define se second #define SZ(x) ((int)(x).size()) #define rep(i,j,k) for (int i=(j);i<=(k);i++) #define per(i,j,k) for (int i=(j);i>=(k);i--) #define pcnt(x) __builtin_popcount(x) mt19937 rnd(time(0)); template<class T>void chkmin(T&x,T y){x=min(x,y);} template<class T>void chkmax(T&x,T y){x=max(x,y);} const ll inf=1000000000000000000; //const ll inf=1000000000; //const ll mod=998244353; //const ll mod=1000000007; const int N=7005; int n,k,m; ll dp[N],g[N],ans[N]; vector<pii>t[N]; bool vis[N]; queue<int>q; int main() { ios_base::sync_with_stdio(false); cin.tie(0);cout.tie(0); cin >> n >> k >> m; rep(i,0,m-1) dp[i]=inf,ans[i]=inf; dp[0]=0; rep(i,1,n) { int x,y,z; cin >> x >> y >> z; t[x].pb(mkp(y,z)); } rep(i,1,k) { rep(j,0,m-1) g[j]=dp[j],dp[j]=inf; rep(j,0,m-1) { for (pii o:t[i]) { chkmin(dp[(j+o.fi)%m],g[j]+o.se); } } } ans[0]=0; vis[0]=1; q.push(0); while (!q.empty()) { int u=q.front(); q.pop(); rep(i,0,m-1) chkmin(ans[(u+i)%m],ans[u]+dp[i]); int v=-1; rep(i,0,m-1) { if (vis[i]) continue; if (v==-1||ans[i]<ans[v]) v=i; } if (v==-1) break; q.push(v); vis[v]=1; } rep(i,0,m-1) { if (ans[i]==inf) cout << "-1\n"; else cout << ans[i] << "\n"; } return 0; } |
English