#include <cstdio> #include <vector> #include <algorithm> #include <utility> using namespace std; const int M = 7'013; const long long inf = 1LL<<61; vector<pair<int, int>> v[M]; int m; int d(int x) { if (x >= m) x-=m; return x; } int main() { int n,k; scanf("%d %d %d", &n, &k, &m); for (int i = 0; i < n; i++) { int ki, mi, wi; scanf("%d %d %d", &ki, &mi, &wi); v[ki].push_back(make_pair(d(mi), wi)); } vector<long long> z(m, inf); z[0] = 0; for (int ki = 1; ki <= k; ki++) { vector<long long> e(m, inf); for(auto [mi, wi] : v[ki]) { for (int i = 0; i < m; i++) { e[d(i+mi)] = min(e[d(i+mi)], z[i] + wi); } } z = e; } vector<pair<long long, int>> u; for (int i = 1; i < m; i++) if(z[i] < inf) u.push_back(make_pair(z[i], i)); sort(u.begin(), u.end()); z[0] = 0; for (auto [wi, mi] : u) { //printf("zestaw %d %lld\n", mi, wi); for (bool on = true; on; ) { on = false; for (int i = 0; i < m ; i++) { if (z[d(i+mi)] > z[i] + wi) { z[d(i+mi)] = z[i] + wi; on = true; } } wi += wi; mi = d(mi+mi); } } for (int i = 0; i < m; i++) { if (z[i] >= inf) z[i] = -1; printf("%lld\n", z[i]); } 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 | #include <cstdio> #include <vector> #include <algorithm> #include <utility> using namespace std; const int M = 7'013; const long long inf = 1LL<<61; vector<pair<int, int>> v[M]; int m; int d(int x) { if (x >= m) x-=m; return x; } int main() { int n,k; scanf("%d %d %d", &n, &k, &m); for (int i = 0; i < n; i++) { int ki, mi, wi; scanf("%d %d %d", &ki, &mi, &wi); v[ki].push_back(make_pair(d(mi), wi)); } vector<long long> z(m, inf); z[0] = 0; for (int ki = 1; ki <= k; ki++) { vector<long long> e(m, inf); for(auto [mi, wi] : v[ki]) { for (int i = 0; i < m; i++) { e[d(i+mi)] = min(e[d(i+mi)], z[i] + wi); } } z = e; } vector<pair<long long, int>> u; for (int i = 1; i < m; i++) if(z[i] < inf) u.push_back(make_pair(z[i], i)); sort(u.begin(), u.end()); z[0] = 0; for (auto [wi, mi] : u) { //printf("zestaw %d %lld\n", mi, wi); for (bool on = true; on; ) { on = false; for (int i = 0; i < m ; i++) { if (z[d(i+mi)] > z[i] + wi) { z[d(i+mi)] = z[i] + wi; on = true; } } wi += wi; mi = d(mi+mi); } } for (int i = 0; i < m; i++) { if (z[i] >= inf) z[i] = -1; printf("%lld\n", z[i]); } return 0; } |