#include <bits/stdc++.h> using namespace std; int main() { cin.tie(0); ios_base::sync_with_stdio(0); int n, k, m; cin >> n >> k >> m; vector <int> sortkolorkow[k]; int tab[n][3]; for (int i = 0; i < n; i++) { cin >> tab[i][0] >> tab[i][1] >> tab[i][2]; tab[i][0]--; sortkolorkow[tab[i][0]].push_back(i); } for (int i = 0; i < k; i++) { if (sortkolorkow[i].size() == 0) { cout << 0 << "\n"; for (int j = 1; j < m; j++) { cout << -1 << "\n"; } return 0; } } int dp[m]; dp[0] = 0; for (int i = 1; i < m; i++) { dp[i] = INT_MAX/2; } int tab2[m][k]; for (int i = 0; i < m; i++) { for (int j = 0; j < k; j++) { tab2[i][j] = -1; } } int suma = 0, koszt = 0; for (int i = 0; i < k; i++) { suma = (suma + tab[sortkolorkow[i][0]][1]) % m; koszt += tab[sortkolorkow[i][0]][2]; } dp[suma] = koszt; for (int i = 0; i < k; i++) { tab2[suma][i] = sortkolorkow[i][0]; } for (int i = 0; i < k; i++) { for (int j = 0; j < sortkolorkow[i].size(); j++) { for (int l = 0; l < m; l++) { if (dp[l] != INT_MAX/2) { dp[(m+l-tab[tab2[l][i]][1]+tab[sortkolorkow[i][j]][1])%m] = min (dp[(m+l-tab[tab2[l][i]][1]+tab[sortkolorkow[i][j]][1])%m], dp[l]-tab[tab2[l][i]][2]+tab[sortkolorkow[i][j]][2]); } } } } /*for (int i = 0; i < m; i++) { cout << dp[i] << "\n"; }*/ vector <pair<int, int>> graf[m]; for (int i = 0; i < m; i++) { for (int j = 0; j < m; j++) { graf[j].push_back({dp[i], (j+i)%m}); graf[i].push_back({dp[j], (j+i)%m}); } } int odleglosc[m]; odleglosc[0] = 0; for (int i = 1; i < m; i++) { odleglosc[i] = INT_MAX/2; } bool pewne_wierzcholki[m]; for (int i = 0; i < m; i++) pewne_wierzcholki[i] = false; priority_queue <pair <int, int>> q; q.push({0, 0}); while (!q.empty()) { pair <int, int> n2 = q.top(); if (pewne_wierzcholki[n2.second] == 0){ n2.first = n2.first * -1; //cout << n2.second << ": "; pewne_wierzcholki[n2.second] = 1; q.pop(); for (int i = 0; i < graf[n2.second].size(); i++) { int n3 = graf[n2.second][i].second; if (pewne_wierzcholki[n3] == 0){ //cout << n3 << ", "; if (odleglosc[n3] > odleglosc[n2.second] + graf[n2.second][i].first) { odleglosc[n3] = odleglosc[n2.second] + graf[n2.second][i].first; q.push(make_pair(odleglosc[n3] * -1, n3)); } } } //cout << "\n"; } } for (int i = 0; i < m; i++) { cout << odleglosc[i] << "\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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | #include <bits/stdc++.h> using namespace std; int main() { cin.tie(0); ios_base::sync_with_stdio(0); int n, k, m; cin >> n >> k >> m; vector <int> sortkolorkow[k]; int tab[n][3]; for (int i = 0; i < n; i++) { cin >> tab[i][0] >> tab[i][1] >> tab[i][2]; tab[i][0]--; sortkolorkow[tab[i][0]].push_back(i); } for (int i = 0; i < k; i++) { if (sortkolorkow[i].size() == 0) { cout << 0 << "\n"; for (int j = 1; j < m; j++) { cout << -1 << "\n"; } return 0; } } int dp[m]; dp[0] = 0; for (int i = 1; i < m; i++) { dp[i] = INT_MAX/2; } int tab2[m][k]; for (int i = 0; i < m; i++) { for (int j = 0; j < k; j++) { tab2[i][j] = -1; } } int suma = 0, koszt = 0; for (int i = 0; i < k; i++) { suma = (suma + tab[sortkolorkow[i][0]][1]) % m; koszt += tab[sortkolorkow[i][0]][2]; } dp[suma] = koszt; for (int i = 0; i < k; i++) { tab2[suma][i] = sortkolorkow[i][0]; } for (int i = 0; i < k; i++) { for (int j = 0; j < sortkolorkow[i].size(); j++) { for (int l = 0; l < m; l++) { if (dp[l] != INT_MAX/2) { dp[(m+l-tab[tab2[l][i]][1]+tab[sortkolorkow[i][j]][1])%m] = min (dp[(m+l-tab[tab2[l][i]][1]+tab[sortkolorkow[i][j]][1])%m], dp[l]-tab[tab2[l][i]][2]+tab[sortkolorkow[i][j]][2]); } } } } /*for (int i = 0; i < m; i++) { cout << dp[i] << "\n"; }*/ vector <pair<int, int>> graf[m]; for (int i = 0; i < m; i++) { for (int j = 0; j < m; j++) { graf[j].push_back({dp[i], (j+i)%m}); graf[i].push_back({dp[j], (j+i)%m}); } } int odleglosc[m]; odleglosc[0] = 0; for (int i = 1; i < m; i++) { odleglosc[i] = INT_MAX/2; } bool pewne_wierzcholki[m]; for (int i = 0; i < m; i++) pewne_wierzcholki[i] = false; priority_queue <pair <int, int>> q; q.push({0, 0}); while (!q.empty()) { pair <int, int> n2 = q.top(); if (pewne_wierzcholki[n2.second] == 0){ n2.first = n2.first * -1; //cout << n2.second << ": "; pewne_wierzcholki[n2.second] = 1; q.pop(); for (int i = 0; i < graf[n2.second].size(); i++) { int n3 = graf[n2.second][i].second; if (pewne_wierzcholki[n3] == 0){ //cout << n3 << ", "; if (odleglosc[n3] > odleglosc[n2.second] + graf[n2.second][i].first) { odleglosc[n3] = odleglosc[n2.second] + graf[n2.second][i].first; q.push(make_pair(odleglosc[n3] * -1, n3)); } } } //cout << "\n"; } } for (int i = 0; i < m; i++) { cout << odleglosc[i] << "\n"; } return 0; } |