#include<iostream> #include<vector> #include<algorithm> using namespace std; typedef long long int ll; const int MX = 7007; const ll oo = 1e18+7; struct zelek{ ll kolor; ll masa; ll cena; }; bool cmp(zelek l, zelek r){ return l.kolor < r.kolor; } ll dp[MX][MX]; vector<zelek> arr; int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, kolory, m; cin >> n >> kolory >> m; for(int i = 0 ; i < n; i++){ zelek a; cin >> a.kolor >> a.masa >> a.cena; arr.push_back(a); } for(int i = 0 ; i < MX; i++){ for(int k = 0 ; k < MX; k++){ dp[i][k] = oo; } } dp[0][0] = 0; sort(arr.begin(), arr.end(), cmp); for(int i = 0; i < n; i++){ for(int k = 0; k < m; k++){ int idx = (k - arr[i].masa)%m; if(idx < 0) idx += m; int kol = arr[i].kolor; dp[kol][k] = min(dp[kol][k], dp[kol-1][idx]+arr[i].cena); } } ll tab[MX][2]; for(int i = 0 ; i < m; i++){ tab[i][0] = dp[kolory][i]; tab[i][1] = tab[i][0]; } tab[0][0] = 0; tab[0][1] = -1; for(int i = 1; i < m; i++){ for(ll k = i; k < m; k++){ ll idx = (i+k)%m; ll sum = tab[i][0] + tab[k][0]; tab[idx][1] = min(tab[idx][1], sum); } } for(int z = 0; z < m; z++){ ll mini = oo; int pos; for(ll i = 0; i < m; i++){ if(tab[i][1] != -1 and tab[i][1] <= mini){ mini = tab[i][1]; pos = i; } } if(mini == oo) break; tab[pos][0] = tab[pos][1]; tab[pos][1] = -1; for(int i = 0; i < m; i++){ int idx = (pos+i)%m; ll sum = tab[pos][0] + tab[i][0]; tab[idx][1] = min(tab[idx][1], sum); } } for(int i = 0 ; i < m; i++){ cout << (tab[i][0] == oo ? -1 : tab[i][0]) << '\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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | #include<iostream> #include<vector> #include<algorithm> using namespace std; typedef long long int ll; const int MX = 7007; const ll oo = 1e18+7; struct zelek{ ll kolor; ll masa; ll cena; }; bool cmp(zelek l, zelek r){ return l.kolor < r.kolor; } ll dp[MX][MX]; vector<zelek> arr; int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, kolory, m; cin >> n >> kolory >> m; for(int i = 0 ; i < n; i++){ zelek a; cin >> a.kolor >> a.masa >> a.cena; arr.push_back(a); } for(int i = 0 ; i < MX; i++){ for(int k = 0 ; k < MX; k++){ dp[i][k] = oo; } } dp[0][0] = 0; sort(arr.begin(), arr.end(), cmp); for(int i = 0; i < n; i++){ for(int k = 0; k < m; k++){ int idx = (k - arr[i].masa)%m; if(idx < 0) idx += m; int kol = arr[i].kolor; dp[kol][k] = min(dp[kol][k], dp[kol-1][idx]+arr[i].cena); } } ll tab[MX][2]; for(int i = 0 ; i < m; i++){ tab[i][0] = dp[kolory][i]; tab[i][1] = tab[i][0]; } tab[0][0] = 0; tab[0][1] = -1; for(int i = 1; i < m; i++){ for(ll k = i; k < m; k++){ ll idx = (i+k)%m; ll sum = tab[i][0] + tab[k][0]; tab[idx][1] = min(tab[idx][1], sum); } } for(int z = 0; z < m; z++){ ll mini = oo; int pos; for(ll i = 0; i < m; i++){ if(tab[i][1] != -1 and tab[i][1] <= mini){ mini = tab[i][1]; pos = i; } } if(mini == oo) break; tab[pos][0] = tab[pos][1]; tab[pos][1] = -1; for(int i = 0; i < m; i++){ int idx = (pos+i)%m; ll sum = tab[pos][0] + tab[i][0]; tab[idx][1] = min(tab[idx][1], sum); } } for(int i = 0 ; i < m; i++){ cout << (tab[i][0] == oo ? -1 : tab[i][0]) << '\n'; } return 0; } |