#include <bits/stdc++.h> using namespace std; #define st first #define nd second typedef long long ll; typedef long double ld; typedef __int128 int128; typedef vector<int> vi; typedef pair<int,int> pi; typedef pair<double,double> pd; typedef pair<ll,ll> pl; const ll max_n = 7007; const ll INF = 1e18+7; ll dp[max_n]; ll dodaj[max_n]; ll akt_num[max_n]; vector<pl> vec[max_n]; vector<pl> reszty; vector<pl> g[max_n]; ll ans[max_n]; bool odw[max_n]; bool on[max_n]; int main(){ ios::sync_with_stdio(0); cin.tie(0); ll n,k,m; cin >> n >> k >> m; for(ll i = 1; i < m; i++){ akt_num[i] = -1; dodaj[i] = INF; } dodaj[0] = INF; for(ll i = 1; i <= n; i++){ ll num,masa,c; cin >> num >> masa >> c; if(masa == m) masa = 0; vec[num].push_back({masa,c}); } for(ll t = 1; t <= k; t++){ //cout << "\nt: " << t << '\n'; for(auto x:vec[t]){ ll masa = x.st; ll c = x.nd; //cout << "\nmasa: " << masa << " cost: " << c << '\n'; for(ll i = 0; i < m; i++){ ll prev = (i-masa+m); if(prev >= m) prev -= m; if(akt_num[prev] != t-1) continue; dodaj[i] = min(dodaj[i],dp[prev]+c); //cout << "dodanie w i: " << i << " val: " << dodaj[i] << '\n'; } } for(ll i = 0; i < m; i++){ if(dodaj[i] == INF) continue; dp[i] = dodaj[i]; akt_num[i] = t; dodaj[i] = INF; } } for(ll i = 0; i < m; i++){ //cout << "i: " << i << " akt_num: " << akt_num[i] // << " dp: " << dp[i] << '\n'; if(akt_num[i] != k) continue; g[m].push_back({i,dp[i]}); reszty.push_back({i,dp[i]}); } for(ll i = 0; i < m; i++){ ans[i] = INF; for(auto x:reszty){ ll next = (i+x.st); if(next >= m) next -= m; g[i].push_back({next,x.nd}); } } on[m] = 1; while(1){ ll v = -1; for(ll i = 0; i <= m; i++){ if(on[i] && (v == -1 || ans[i] < ans[v])) v = i; } if(v == -1) break; on[v] = 0; for(auto x:g[v]){ //if(odw[x.st]) continue; if(ans[v]+x.nd < ans[x.st]){ ans[x.st] = ans[v]+x.nd; on[x.st] = 1; } } } cout << "0\n"; //dla r = 0 for(ll i = 1; i < m; i++){ if(ans[i] == INF) cout << "-1\n"; else cout << ans[i] << '\n'; } return 0; } //g++ -O3 -static -Wall .cpp -std=c++17
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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | #include <bits/stdc++.h> using namespace std; #define st first #define nd second typedef long long ll; typedef long double ld; typedef __int128 int128; typedef vector<int> vi; typedef pair<int,int> pi; typedef pair<double,double> pd; typedef pair<ll,ll> pl; const ll max_n = 7007; const ll INF = 1e18+7; ll dp[max_n]; ll dodaj[max_n]; ll akt_num[max_n]; vector<pl> vec[max_n]; vector<pl> reszty; vector<pl> g[max_n]; ll ans[max_n]; bool odw[max_n]; bool on[max_n]; int main(){ ios::sync_with_stdio(0); cin.tie(0); ll n,k,m; cin >> n >> k >> m; for(ll i = 1; i < m; i++){ akt_num[i] = -1; dodaj[i] = INF; } dodaj[0] = INF; for(ll i = 1; i <= n; i++){ ll num,masa,c; cin >> num >> masa >> c; if(masa == m) masa = 0; vec[num].push_back({masa,c}); } for(ll t = 1; t <= k; t++){ //cout << "\nt: " << t << '\n'; for(auto x:vec[t]){ ll masa = x.st; ll c = x.nd; //cout << "\nmasa: " << masa << " cost: " << c << '\n'; for(ll i = 0; i < m; i++){ ll prev = (i-masa+m); if(prev >= m) prev -= m; if(akt_num[prev] != t-1) continue; dodaj[i] = min(dodaj[i],dp[prev]+c); //cout << "dodanie w i: " << i << " val: " << dodaj[i] << '\n'; } } for(ll i = 0; i < m; i++){ if(dodaj[i] == INF) continue; dp[i] = dodaj[i]; akt_num[i] = t; dodaj[i] = INF; } } for(ll i = 0; i < m; i++){ //cout << "i: " << i << " akt_num: " << akt_num[i] // << " dp: " << dp[i] << '\n'; if(akt_num[i] != k) continue; g[m].push_back({i,dp[i]}); reszty.push_back({i,dp[i]}); } for(ll i = 0; i < m; i++){ ans[i] = INF; for(auto x:reszty){ ll next = (i+x.st); if(next >= m) next -= m; g[i].push_back({next,x.nd}); } } on[m] = 1; while(1){ ll v = -1; for(ll i = 0; i <= m; i++){ if(on[i] && (v == -1 || ans[i] < ans[v])) v = i; } if(v == -1) break; on[v] = 0; for(auto x:g[v]){ //if(odw[x.st]) continue; if(ans[v]+x.nd < ans[x.st]){ ans[x.st] = ans[v]+x.nd; on[x.st] = 1; } } } cout << "0\n"; //dla r = 0 for(ll i = 1; i < m; i++){ if(ans[i] == INF) cout << "-1\n"; else cout << ans[i] << '\n'; } return 0; } //g++ -O3 -static -Wall .cpp -std=c++17 |