#include <bits/stdc++.h> #define pb emplace_back #define FOR(i,a,b) for(int i = a; i < b;++i) #define int long long #define mp make_pair #define f first #define s second using namespace std; const int inf = 1e17; int32_t main(){ cin.tie(0); ios_base::sync_with_stdio(0); int n,k,m; cin>>n >>k >>m; vector<int> dist(m + 1,inf),vis(m + 3,0); vector<pair<int,int>> V[k + 5],Q; FOR(i,0,n){ int k1,m1,w1; cin>>k1 >>m1 >>w1; V[k1].pb(mp(m1,w1)); } FOR(i,1,k + 1){ if(V[i].size() == 0){ cout<<0 <<"\n"; FOR(j,0,m - 1){ cout<<-1 <<"\n"; } return 0; } } int dp[k + 5][m + 5]; FOR(i,0,k + 3){ FOR(j,0,m + 3){ dp[i][j] = inf; } } dp[1][0] = 0; FOR(i,1,k + 1){ if(V[i].size() == 0){ FOR(j,0,m + 1){ dp[i + 1][j] = dp[i][j]; } }else{ for(auto &y : V[i]){ FOR(j,0,m + 1){ dp[i + 1][(j + y.f) % m] = min(dp[i + 1][(j + y.f) % m],dp[i][j] + y.s); } } } } dist[0] = 0; FOR(i,0,m){ int wyn = 1e17,kto = -1; FOR(j,0,m){ if(vis[j] == 0 && dist[j] < wyn){ wyn = dist[j]; kto = j; } } if(kto != -1){ vis[kto] = 1; FOR(j,0,m){ if(dist[kto] + dp[k + 1][j] < dist[(kto + j) % m]){ dist[(kto + j) % m] = dist[kto] + dp[k + 1][j]; } } } } FOR(i,0,m){ if(dist[i] == 1e17){ cout<<-1; }else{ cout<<dist[i]; } cout<<"\n"; } }
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 | #include <bits/stdc++.h> #define pb emplace_back #define FOR(i,a,b) for(int i = a; i < b;++i) #define int long long #define mp make_pair #define f first #define s second using namespace std; const int inf = 1e17; int32_t main(){ cin.tie(0); ios_base::sync_with_stdio(0); int n,k,m; cin>>n >>k >>m; vector<int> dist(m + 1,inf),vis(m + 3,0); vector<pair<int,int>> V[k + 5],Q; FOR(i,0,n){ int k1,m1,w1; cin>>k1 >>m1 >>w1; V[k1].pb(mp(m1,w1)); } FOR(i,1,k + 1){ if(V[i].size() == 0){ cout<<0 <<"\n"; FOR(j,0,m - 1){ cout<<-1 <<"\n"; } return 0; } } int dp[k + 5][m + 5]; FOR(i,0,k + 3){ FOR(j,0,m + 3){ dp[i][j] = inf; } } dp[1][0] = 0; FOR(i,1,k + 1){ if(V[i].size() == 0){ FOR(j,0,m + 1){ dp[i + 1][j] = dp[i][j]; } }else{ for(auto &y : V[i]){ FOR(j,0,m + 1){ dp[i + 1][(j + y.f) % m] = min(dp[i + 1][(j + y.f) % m],dp[i][j] + y.s); } } } } dist[0] = 0; FOR(i,0,m){ int wyn = 1e17,kto = -1; FOR(j,0,m){ if(vis[j] == 0 && dist[j] < wyn){ wyn = dist[j]; kto = j; } } if(kto != -1){ vis[kto] = 1; FOR(j,0,m){ if(dist[kto] + dp[k + 1][j] < dist[(kto + j) % m]){ dist[(kto + j) % m] = dist[kto] + dp[k + 1][j]; } } } } FOR(i,0,m){ if(dist[i] == 1e17){ cout<<-1; }else{ cout<<dist[i]; } cout<<"\n"; } } |