#include <stdio.h> #include <time.h> #include <math.h> #include <stdlib.h> #include <string.h> #include <map> #include <string> #include <vector> #include <iostream> #include <sstream> #include <queue> #include <algorithm> #include <assert.h> using namespace std; #define ll long long #define PB push_back #define FOR(a,start,end) for(int a=int(start); a<int(end); a++) #define INF INT_MAX #define SORT(a) sort(a.begin(),a.end()) #define CL(a,x) memset(a,x,sizeof(a)) #define REP(a,x) for(int a=0;a<x;a++) #define REP1(a,x) for(int a=1;a<=x;a++) #define MP make_pair typedef vector<ll> vi; typedef pair<ll, ll> pii; typedef vector<string> vs; typedef vector<pii> vii; typedef vector<vector<ll> > vvi; typedef vector<vector<string> > vvs; typedef vector<pair<string, string> > vss; typedef pair<string, string> pss; typedef pair<ll, pii> ppii; typedef vector<ppii> vppii; typedef vector<vector<pii> > vvii; typedef vector<vvi> vvvi; ll n, k, q, d, r, g, b, z, p, a, c, m; char zn; string wa; vi vk, va, vb, vc, vc2; vii vkx, vkx2; vvii vvkx; pii kompl(pii p0, int i0) { if (i0 == k + 1) { if (p0.second < vb[p0.first]) { vb[p0.first] = p0.second; } return p0; } pii p2 = p0; for (int j = 0; j < (int)vvkx[i0].size(); j++) { p0.first += vvkx[i0][j].first; p0.second += vvkx[i0][j].second; p0.first %= m; pii p1 = kompl(p0, i0 + 1); p0 = p2; } return p0; } int main() { //freopen("c:\\wojtek\\uva\\pa\\debug\\t2.in", "rt", stdin); //d = 1000000007; std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); // while (1) { cin >> n >> k >> m; vkx.clear(); vvkx.assign(k + 1, vkx); vb.assign(m, 10000000000000000ll); //vc[0] = 0; for (int i = 0; i < n; i++) { cin >> a >> b >> c; vvkx[a].push_back(MP(b % m, c)); } for (int i = 1; i <= k; i++) { if ((int)vvkx[i].size() == 0) { vb.assign(m, -1); vb[0] = 0; break; } else { SORT(vvkx[i]); for (int j = 1; j < (int)vvkx[i].size(); j++) { if (vvkx[i][j].first == vvkx[i][j - 1].first) { if (vvkx[i][j].second < vvkx[i][j - 1].second) vvkx[i].erase(vvkx[i].begin() + j - 1); else vvkx[i].erase(vvkx[i].begin() + j); j--; } } } } if (vb[0] != 0) { vb.assign(m, 10000000000000000ll); for (int j = 0; j < (int)vvkx[1].size(); j++) { pii p0 = kompl(vvkx[1][j], 2); } g = 0; d = 10000000000000000ll; for (int i = 0; i < m; i++) { if (vb[i] < 10000000000000000ll) { vkx.push_back(MP(i, vb[i])); g = max(g, vb[i]); d = min(d, vb[i]); } } vc = vb; vc2.assign(m, 10000000000000000ll); for (int ix = 0; ix < (g / d) + 1; ix++) { for (int i = 0; i < m; i++) { if (vc[i] < 10000000000000000ll) { for (int j = 0; j < (int)vkx.size(); j++) { if (vb[(vkx[j].first + i) % m] > vc[i] + vkx[j].second) vb[(i + vkx[j].first) % m] = vc[i] + vkx[j].second; vc2[(i + vkx[j].first) % m] = min(vc2[(i + vkx[j].first) % m], vc[i] + vkx[j].second); } } } vc2.swap(vc); vc2.assign(m, 10000000000000000ll); } } cout << 0 << '\n'; for (int i = 1; i < m; i++) { if (vb[i] == 10000000000000000ll) cout << -1 << '\n'; else cout << vb[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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | #include <stdio.h> #include <time.h> #include <math.h> #include <stdlib.h> #include <string.h> #include <map> #include <string> #include <vector> #include <iostream> #include <sstream> #include <queue> #include <algorithm> #include <assert.h> using namespace std; #define ll long long #define PB push_back #define FOR(a,start,end) for(int a=int(start); a<int(end); a++) #define INF INT_MAX #define SORT(a) sort(a.begin(),a.end()) #define CL(a,x) memset(a,x,sizeof(a)) #define REP(a,x) for(int a=0;a<x;a++) #define REP1(a,x) for(int a=1;a<=x;a++) #define MP make_pair typedef vector<ll> vi; typedef pair<ll, ll> pii; typedef vector<string> vs; typedef vector<pii> vii; typedef vector<vector<ll> > vvi; typedef vector<vector<string> > vvs; typedef vector<pair<string, string> > vss; typedef pair<string, string> pss; typedef pair<ll, pii> ppii; typedef vector<ppii> vppii; typedef vector<vector<pii> > vvii; typedef vector<vvi> vvvi; ll n, k, q, d, r, g, b, z, p, a, c, m; char zn; string wa; vi vk, va, vb, vc, vc2; vii vkx, vkx2; vvii vvkx; pii kompl(pii p0, int i0) { if (i0 == k + 1) { if (p0.second < vb[p0.first]) { vb[p0.first] = p0.second; } return p0; } pii p2 = p0; for (int j = 0; j < (int)vvkx[i0].size(); j++) { p0.first += vvkx[i0][j].first; p0.second += vvkx[i0][j].second; p0.first %= m; pii p1 = kompl(p0, i0 + 1); p0 = p2; } return p0; } int main() { //freopen("c:\\wojtek\\uva\\pa\\debug\\t2.in", "rt", stdin); //d = 1000000007; std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); // while (1) { cin >> n >> k >> m; vkx.clear(); vvkx.assign(k + 1, vkx); vb.assign(m, 10000000000000000ll); //vc[0] = 0; for (int i = 0; i < n; i++) { cin >> a >> b >> c; vvkx[a].push_back(MP(b % m, c)); } for (int i = 1; i <= k; i++) { if ((int)vvkx[i].size() == 0) { vb.assign(m, -1); vb[0] = 0; break; } else { SORT(vvkx[i]); for (int j = 1; j < (int)vvkx[i].size(); j++) { if (vvkx[i][j].first == vvkx[i][j - 1].first) { if (vvkx[i][j].second < vvkx[i][j - 1].second) vvkx[i].erase(vvkx[i].begin() + j - 1); else vvkx[i].erase(vvkx[i].begin() + j); j--; } } } } if (vb[0] != 0) { vb.assign(m, 10000000000000000ll); for (int j = 0; j < (int)vvkx[1].size(); j++) { pii p0 = kompl(vvkx[1][j], 2); } g = 0; d = 10000000000000000ll; for (int i = 0; i < m; i++) { if (vb[i] < 10000000000000000ll) { vkx.push_back(MP(i, vb[i])); g = max(g, vb[i]); d = min(d, vb[i]); } } vc = vb; vc2.assign(m, 10000000000000000ll); for (int ix = 0; ix < (g / d) + 1; ix++) { for (int i = 0; i < m; i++) { if (vc[i] < 10000000000000000ll) { for (int j = 0; j < (int)vkx.size(); j++) { if (vb[(vkx[j].first + i) % m] > vc[i] + vkx[j].second) vb[(i + vkx[j].first) % m] = vc[i] + vkx[j].second; vc2[(i + vkx[j].first) % m] = min(vc2[(i + vkx[j].first) % m], vc[i] + vkx[j].second); } } } vc2.swap(vc); vc2.assign(m, 10000000000000000ll); } } cout << 0 << '\n'; for (int i = 1; i < m; i++) { if (vb[i] == 10000000000000000ll) cout << -1 << '\n'; else cout << vb[i] << '\n'; } //} return 0; } |