#include <bits/stdc++.h> using namespace std; #define endl "\n" #define ll long long #define ld long double #define ull unsigned long long void dbg_out() { cout << endl; } template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cout << ' ' << H; dbg_out(T...); } #define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__) void solve() { ll n,k,m,a,b,c; cin >> n >> k >> m; vector<vector<pair<ll, ll>>> list(k+1); vector<vector<ll>> res(k+2, vector<ll>(m, -1)); unordered_map<ll, ll> ans; for(ll i = 0; i < m; i++) ans[i] = -1; ans[0] = 0; // for(auto el : ans) cout << el.first << " -> " << el.second << endl; for(ll i = 0; i < n; i++){ cin >> a >> b >> c; list[a].push_back({b,c}); } res[k+1][0] = 0; // for(auto sub : list){ // for(auto el : sub) cout << "(" << el.first << " " << el.second << ")"; // cout << endl; // } ll cnt=0; for(ll i = k; i >= 0; i--){ for(ll j = 0; j < list[i].size(); j++){ for(ll index = 0; index < m; index++){ if(res[i+1][index] != -1){ if(res[i][(index+list[i][j].first) % m] == -1){ res[i][(index+list[i][j].first) % m] = res[i+1][index] + list[i][j].second; } else{ res[i][(index+list[i][j].first) % m] = min(res[i][(index+list[i][j].first) % m], res[i+1][index] + list[i][j].second); } } // cnt++; // dbg(i+2, x, index); // dbg(res[i+2][x][index]); // for(auto el : res[i+2][x]) cout << el << " "; // cout << endl; // if(res[i+1][x][index] != -1){ // res[i][j][(index+list[i][j].first) % m] = res[i+1][x][index] + list[i][j].second; // } } // dbg(res[i].size(), res[i+1].size(), res[i].size()*res[i+1].size(), cnt); } } // for(auto sub : res){ // for(auto el : sub)cout << el << " "; // cout << endl; // cout << "--------\n"; // } // cout << cnt << endl; // // cout << endl << endl; // // for(auto sub : res){ // // for(auto el1 : sub){ // // for(auto el : el1) cout << el << " "; // // cout << endl; // // } // // cout << "--------\n"; // // } for(ll i = 0; i < m; i++){ if(res[1][i] == -1) continue; if(ans[i] != -1){ ans[i] = min(ans[i], res[1][i]); } else{ ans[i] = res[1][i]; } } // // for(auto el : ans) cout << el.first << " -> " << el.second << endl; // // cout << endl; bool flag = true; while(flag){ flag = false; for(auto el1 : ans){ for(auto el2: ans){ if(el1.second == -1 || el2.second == -1) continue; if((ans[(el1.first+el2.first)%m] == -1) || (ans[(el1.first+el2.first)%m] > el1.second+el2.second)){ flag = true; ans[(el1.first+el2.first)%m] = el1.second+el2.second; } } } } for(ll i = 0; i < m; i++) cout << ans[i] << endl; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); // #ifndef ONLINE_JUDGE // freopen("../../in.in", "r", stdin); // freopen("../../out.out", "w", stdout); // #endif solve(); }
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 | #include <bits/stdc++.h> using namespace std; #define endl "\n" #define ll long long #define ld long double #define ull unsigned long long void dbg_out() { cout << endl; } template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cout << ' ' << H; dbg_out(T...); } #define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__) void solve() { ll n,k,m,a,b,c; cin >> n >> k >> m; vector<vector<pair<ll, ll>>> list(k+1); vector<vector<ll>> res(k+2, vector<ll>(m, -1)); unordered_map<ll, ll> ans; for(ll i = 0; i < m; i++) ans[i] = -1; ans[0] = 0; // for(auto el : ans) cout << el.first << " -> " << el.second << endl; for(ll i = 0; i < n; i++){ cin >> a >> b >> c; list[a].push_back({b,c}); } res[k+1][0] = 0; // for(auto sub : list){ // for(auto el : sub) cout << "(" << el.first << " " << el.second << ")"; // cout << endl; // } ll cnt=0; for(ll i = k; i >= 0; i--){ for(ll j = 0; j < list[i].size(); j++){ for(ll index = 0; index < m; index++){ if(res[i+1][index] != -1){ if(res[i][(index+list[i][j].first) % m] == -1){ res[i][(index+list[i][j].first) % m] = res[i+1][index] + list[i][j].second; } else{ res[i][(index+list[i][j].first) % m] = min(res[i][(index+list[i][j].first) % m], res[i+1][index] + list[i][j].second); } } // cnt++; // dbg(i+2, x, index); // dbg(res[i+2][x][index]); // for(auto el : res[i+2][x]) cout << el << " "; // cout << endl; // if(res[i+1][x][index] != -1){ // res[i][j][(index+list[i][j].first) % m] = res[i+1][x][index] + list[i][j].second; // } } // dbg(res[i].size(), res[i+1].size(), res[i].size()*res[i+1].size(), cnt); } } // for(auto sub : res){ // for(auto el : sub)cout << el << " "; // cout << endl; // cout << "--------\n"; // } // cout << cnt << endl; // // cout << endl << endl; // // for(auto sub : res){ // // for(auto el1 : sub){ // // for(auto el : el1) cout << el << " "; // // cout << endl; // // } // // cout << "--------\n"; // // } for(ll i = 0; i < m; i++){ if(res[1][i] == -1) continue; if(ans[i] != -1){ ans[i] = min(ans[i], res[1][i]); } else{ ans[i] = res[1][i]; } } // // for(auto el : ans) cout << el.first << " -> " << el.second << endl; // // cout << endl; bool flag = true; while(flag){ flag = false; for(auto el1 : ans){ for(auto el2: ans){ if(el1.second == -1 || el2.second == -1) continue; if((ans[(el1.first+el2.first)%m] == -1) || (ans[(el1.first+el2.first)%m] > el1.second+el2.second)){ flag = true; ans[(el1.first+el2.first)%m] = el1.second+el2.second; } } } } for(ll i = 0; i < m; i++) cout << ans[i] << endl; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); // #ifndef ONLINE_JUDGE // freopen("../../in.in", "r", stdin); // freopen("../../out.out", "w", stdout); // #endif solve(); } |