#include<iostream> #include<vector> #include<queue> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n; // rodzaje long long k; // kolory long long m; // modulo cin >> n; cin >> k; cin >> m; vector<vector<long long>> wagi(k + 1); vector<vector<long long>> ceny(k + 1); long long kolor; long long waga; long long cena; for(long long i = 0; i < n; i++) { cin >> kolor; cin >> waga; cin >> cena; waga %= m; wagi[kolor].push_back(waga); ceny[kolor].push_back(cena); } vector<vector<long long>> dp(k + 1, vector<long long>(m)); if(ceny[1].size() == 0) { cout<<"0"; return 0; } for(long long i = 0; i <= k; i++) { for(long long j = 0; j < m; j++) { dp[i][j] = -1; } } dp[0][0] = 0; for(long long i = 1; i <= k; i++) { for(long long j = 0; j < ceny[i].size(); j++) { for(long long a = 0; a < m; a++) { if(dp[i-1][a] == -1) { continue; } long long w = a + wagi[i][j]; w %= m; if(dp[i][w] == -1) { dp[i][w] = dp[i - 1][a] + ceny[i][j]; } dp[i][w] = min(dp[i][w], dp[i - 1][a] + ceny[i][j]); } } } dp[k][0] = 0; vector<long long> wyniki = dp[k]; queue<long long> p1; queue<long long> p2; for(long long i = 1; i < wyniki.size(); i++) { if(wyniki[i] != -1) { p1.push(i); } } while(p1.empty() == false) { while(p1.empty() == false) { long long w = p1.front(); p1.pop(); for(long long i = 1; i < m; i++) { if(wyniki[i] != -1) { long long waga = i + w; waga %= m; if(wyniki[waga] == -1) { wyniki[waga] = wyniki[w] + wyniki[i]; p2.push(waga); } if(wyniki[waga] > wyniki[w] + wyniki[i]) { wyniki[waga] = wyniki[w] + wyniki[i]; p2.push(waga); } } } } p1 = p2; while(p2.empty() == false) { p2.pop(); } } for(long long i = 0; i < m; i++) { cout<<wyniki[i]<<endl; } 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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | #include<iostream> #include<vector> #include<queue> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n; // rodzaje long long k; // kolory long long m; // modulo cin >> n; cin >> k; cin >> m; vector<vector<long long>> wagi(k + 1); vector<vector<long long>> ceny(k + 1); long long kolor; long long waga; long long cena; for(long long i = 0; i < n; i++) { cin >> kolor; cin >> waga; cin >> cena; waga %= m; wagi[kolor].push_back(waga); ceny[kolor].push_back(cena); } vector<vector<long long>> dp(k + 1, vector<long long>(m)); if(ceny[1].size() == 0) { cout<<"0"; return 0; } for(long long i = 0; i <= k; i++) { for(long long j = 0; j < m; j++) { dp[i][j] = -1; } } dp[0][0] = 0; for(long long i = 1; i <= k; i++) { for(long long j = 0; j < ceny[i].size(); j++) { for(long long a = 0; a < m; a++) { if(dp[i-1][a] == -1) { continue; } long long w = a + wagi[i][j]; w %= m; if(dp[i][w] == -1) { dp[i][w] = dp[i - 1][a] + ceny[i][j]; } dp[i][w] = min(dp[i][w], dp[i - 1][a] + ceny[i][j]); } } } dp[k][0] = 0; vector<long long> wyniki = dp[k]; queue<long long> p1; queue<long long> p2; for(long long i = 1; i < wyniki.size(); i++) { if(wyniki[i] != -1) { p1.push(i); } } while(p1.empty() == false) { while(p1.empty() == false) { long long w = p1.front(); p1.pop(); for(long long i = 1; i < m; i++) { if(wyniki[i] != -1) { long long waga = i + w; waga %= m; if(wyniki[waga] == -1) { wyniki[waga] = wyniki[w] + wyniki[i]; p2.push(waga); } if(wyniki[waga] > wyniki[w] + wyniki[i]) { wyniki[waga] = wyniki[w] + wyniki[i]; p2.push(waga); } } } } p1 = p2; while(p2.empty() == false) { p2.pop(); } } for(long long i = 0; i < m; i++) { cout<<wyniki[i]<<endl; } return 0; } |