#include <bits/stdc++.h> #pragma GCC optimize("Ofast,unroll-loops") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,fma,abm,mmx,avx,avx2,tune=native") #pragma GCC target("sse,sse2,abm,mmx,sse3,tune=native") using namespace std; typedef long long lld; typedef pair<int, int> pii; typedef pair<lld, lld> pll; #define ff first #define dd second #define mp make_pair #define pb push_back #define sz size() #define For(i, s, a) for(int i = s; i < a; ++i) #define all(x) (x).begin(), (x).end() #define make_unique(x) (x).erase(unique(all(x)), (x).end()) using tii = tuple<int, int, int>; vector<tii> input; lld inf = 0x3f3f3f3f3f3f3f3f; void dfsxd(int n, int k, int m, int x, lld c, vector<int> pos, int d = 0) { For (i, 0, n) { if (get<0>(input[i]) != 1) continue; if (i % 100 == 0) cout << c << " gowno " << d << endl; if (c + get<2>(input[i]) > 64261877) continue; For (j, 0, n) { if (get<0>(input[j]) != 2) continue; if (c + get<2>(input[i]) + get<2>(input[j])> 64261877) continue; if (j % 100 == 0) cout << "j" << endl; For (z, 0, n) { if (get<0>(input[z]) != 3) continue; int xd = (x + get<1>(input[i]) + get<1>(input[j]) + get<1>(input[z])) % m; lld xc = c + get<2>(input[i]) + get<2>(input[j]) + get<2>(input[z]); if (xc > 64261877) continue; vector<int> px = pos; px.push_back(i); px.push_back(j); px.push_back(z); if (xd == 142) { cout << "dupsko kasi mielnik " << xc << endl; for (auto ss : px) cout << ss << " " << get<1>(input[ss]) << " " << get<2>(input[ss]) << endl; } if (d < 4) { dfsxd(n, k, m, xd, xc, px, d + 1); } } } } } int main() { int n, k, m; scanf("%d%d%d", &n, &k, &m); For(i, 0, n) { int kk, ms, c; scanf("%d%d%d", &kk, &ms, &c); ms = ms % m; input.pb({kk, ms, c}); } sort(all(input)); vector<lld> masses[2] = {vector<lld>(m, 0), vector<lld>(m, 0)}; For (i, 0, m) masses[0][i] = masses[1][i] = inf; int sw = 1, sm_diff_col = 0; For (i, 0, n) { if (!i || get<0>(input[i]) != get<0>(input[i - 1])) { sw ^= 1; sm_diff_col++; For (j, 0, m) masses[sw][j] = inf; } int mass = get<1>(input[i]), cost = get<2>(input[i]); if (sm_diff_col == 1) { masses[sw][mass] = min(masses[sw][mass], (lld)cost); } For (j, 0, m) { int massnew = (j + mass) % m; masses[sw][massnew] = masses[sw][massnew] > masses[sw ^ 1][j] + cost ? masses[sw ^ 1][j] + cost : masses[sw][massnew]; } } if (sm_diff_col != k) { For (j, 0, m) masses[sw][j] = inf; } priority_queue<pii> q; For (j, 0, m) if (masses[sw][j] != inf) q.push({-masses[sw][j], j}); while (!q.empty()) { int x = q.top().dd; q.pop(); int step = x - 1; For (j, 0, m) { ++step; if (step == m) step = 0; if (masses[sw][step] > masses[sw][j] + masses[sw][x]) { masses[sw][step] = masses[sw][j] + masses[sw][x]; q.push({-masses[sw][step], step}); } } } masses[sw][0] = 0; For (i, 0, m) { printf("%lld\n", masses[sw][i] == inf ? -1 : masses[sw][i]); } }
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 125 126 127 128 129 130 131 132 | #include <bits/stdc++.h> #pragma GCC optimize("Ofast,unroll-loops") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,fma,abm,mmx,avx,avx2,tune=native") #pragma GCC target("sse,sse2,abm,mmx,sse3,tune=native") using namespace std; typedef long long lld; typedef pair<int, int> pii; typedef pair<lld, lld> pll; #define ff first #define dd second #define mp make_pair #define pb push_back #define sz size() #define For(i, s, a) for(int i = s; i < a; ++i) #define all(x) (x).begin(), (x).end() #define make_unique(x) (x).erase(unique(all(x)), (x).end()) using tii = tuple<int, int, int>; vector<tii> input; lld inf = 0x3f3f3f3f3f3f3f3f; void dfsxd(int n, int k, int m, int x, lld c, vector<int> pos, int d = 0) { For (i, 0, n) { if (get<0>(input[i]) != 1) continue; if (i % 100 == 0) cout << c << " gowno " << d << endl; if (c + get<2>(input[i]) > 64261877) continue; For (j, 0, n) { if (get<0>(input[j]) != 2) continue; if (c + get<2>(input[i]) + get<2>(input[j])> 64261877) continue; if (j % 100 == 0) cout << "j" << endl; For (z, 0, n) { if (get<0>(input[z]) != 3) continue; int xd = (x + get<1>(input[i]) + get<1>(input[j]) + get<1>(input[z])) % m; lld xc = c + get<2>(input[i]) + get<2>(input[j]) + get<2>(input[z]); if (xc > 64261877) continue; vector<int> px = pos; px.push_back(i); px.push_back(j); px.push_back(z); if (xd == 142) { cout << "dupsko kasi mielnik " << xc << endl; for (auto ss : px) cout << ss << " " << get<1>(input[ss]) << " " << get<2>(input[ss]) << endl; } if (d < 4) { dfsxd(n, k, m, xd, xc, px, d + 1); } } } } } int main() { int n, k, m; scanf("%d%d%d", &n, &k, &m); For(i, 0, n) { int kk, ms, c; scanf("%d%d%d", &kk, &ms, &c); ms = ms % m; input.pb({kk, ms, c}); } sort(all(input)); vector<lld> masses[2] = {vector<lld>(m, 0), vector<lld>(m, 0)}; For (i, 0, m) masses[0][i] = masses[1][i] = inf; int sw = 1, sm_diff_col = 0; For (i, 0, n) { if (!i || get<0>(input[i]) != get<0>(input[i - 1])) { sw ^= 1; sm_diff_col++; For (j, 0, m) masses[sw][j] = inf; } int mass = get<1>(input[i]), cost = get<2>(input[i]); if (sm_diff_col == 1) { masses[sw][mass] = min(masses[sw][mass], (lld)cost); } For (j, 0, m) { int massnew = (j + mass) % m; masses[sw][massnew] = masses[sw][massnew] > masses[sw ^ 1][j] + cost ? masses[sw ^ 1][j] + cost : masses[sw][massnew]; } } if (sm_diff_col != k) { For (j, 0, m) masses[sw][j] = inf; } priority_queue<pii> q; For (j, 0, m) if (masses[sw][j] != inf) q.push({-masses[sw][j], j}); while (!q.empty()) { int x = q.top().dd; q.pop(); int step = x - 1; For (j, 0, m) { ++step; if (step == m) step = 0; if (masses[sw][step] > masses[sw][j] + masses[sw][x]) { masses[sw][step] = masses[sw][j] + masses[sw][x]; q.push({-masses[sw][step], step}); } } } masses[sw][0] = 0; For (i, 0, m) { printf("%lld\n", masses[sw][i] == inf ? -1 : masses[sw][i]); } } |