#include <iostream>
#include <vector>
#define nl '\n'
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
const ll inf = 1ll<<60;
ll dp[7001][7000];
ll dist[7000];
bool used[7000];
vector<pll> jello[7000];
int n, m, k;
int main()
{
cin.tie(0)->sync_with_stdio(0);
cin>>n>>k>>m;
for(int i=0; i<n; i++){
int col, mass, cost;
cin>>col>>mass>>cost;
jello[col].push_back({mass, cost});
}
for(int i=0; i<=k; i++){
for(int j=0; j<m; j++){
dp[i][j] = inf;
}
}
dp[0][0] = 0;
for(int i=1; i<=k; i++){
for(auto [mass, cost]: jello[i]){
for(int j=0; j<m; j++){
dp[i][(j+mass)%m] = min(dp[i][(j+mass)%m], dp[i-1][j] + cost);
}
}
}
/*for(int i=0; i<=k; i++){
for(int j=0; j<m; j++){
cerr<<dp[i][j]<<' ';
}
cerr<<nl;
}*/
for(int i=1; i<m; i++) dist[i] = inf;
for(int i=0; i<m; i++){
int v = -1; ll dst = inf;
for(int i=0; i<m; i++){
if(!used[i] && dist[i] < dst){ v = i, dst = dist[v]; }
}
if(dst == inf) break;
used[v] = true;
for(int i=0; i<m; i++){
dist[(v+i)%m] = min(dist[(v+i)%m], dst+dp[k][i]);
}
}
for(int i=0; i<m; i++){
cout<<(dist[i] == inf ? -1 : dist[i])<<nl;
}
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 | #include <iostream> #include <vector> #define nl '\n' using namespace std; using ll = long long; using pll = pair<ll, ll>; const ll inf = 1ll<<60; ll dp[7001][7000]; ll dist[7000]; bool used[7000]; vector<pll> jello[7000]; int n, m, k; int main() { cin.tie(0)->sync_with_stdio(0); cin>>n>>k>>m; for(int i=0; i<n; i++){ int col, mass, cost; cin>>col>>mass>>cost; jello[col].push_back({mass, cost}); } for(int i=0; i<=k; i++){ for(int j=0; j<m; j++){ dp[i][j] = inf; } } dp[0][0] = 0; for(int i=1; i<=k; i++){ for(auto [mass, cost]: jello[i]){ for(int j=0; j<m; j++){ dp[i][(j+mass)%m] = min(dp[i][(j+mass)%m], dp[i-1][j] + cost); } } } /*for(int i=0; i<=k; i++){ for(int j=0; j<m; j++){ cerr<<dp[i][j]<<' '; } cerr<<nl; }*/ for(int i=1; i<m; i++) dist[i] = inf; for(int i=0; i<m; i++){ int v = -1; ll dst = inf; for(int i=0; i<m; i++){ if(!used[i] && dist[i] < dst){ v = i, dst = dist[v]; } } if(dst == inf) break; used[v] = true; for(int i=0; i<m; i++){ dist[(v+i)%m] = min(dist[(v+i)%m], dst+dp[k][i]); } } for(int i=0; i<m; i++){ cout<<(dist[i] == inf ? -1 : dist[i])<<nl; } return 0; } |
English