// #include <bits/stdc++.h> #define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); #include <iostream> #include <unordered_map> #include <vector> #include <queue> using namespace std; void failure_fine(int m) { cout <<"0\n"; for (int i = 1; i < m; i++) { cout << "-1\n"; } } struct job { int64_t m; int64_t c; }; int main() { FAST uint16_t n, k, m; cin >> n >> k >> m; uint16_t ki; int64_t mi; int64_t ci; vector<unordered_map<int64_t, int64_t>> opts(k); for (int i = 0; i < n; i++) { cin >> ki >> mi >> ci; ki--; if (mi == m) { mi = 0; } if (opts[ki].count(mi) == 0 || opts[ki][mi]>ci) { opts[ki][mi] = ci; } } unordered_map<int64_t, int64_t> calced; calced.reserve(m); int64_t tmp_c; int64_t tmp_m; unordered_map<int64_t, int64_t> tmp; tmp.reserve(m); for (auto v: opts) { tmp.clear(); if (v.size() == 0) { failure_fine(m); return 0; } if (calced.size() == 0) { for (auto nv: v) { calced[nv.first] = nv.second; } continue; } for (auto ov: calced) { for (auto nv: v) { tmp_m = nv.first + ov.first; if (tmp_m >=m) { tmp_m -= m; } tmp_c = nv.second + ov.second; if (tmp[tmp_m] == 0 || tmp[tmp_m] > tmp_c) { tmp[tmp_m] = tmp_c; } } } calced = tmp; } queue<job> to_check; for (auto v: calced) { to_check.push(job{ m: v.first, c: v.second, }); } job next; while (to_check.size()) { next = to_check.front(); to_check.pop(); if (calced[next.m] < next.c) { continue; } for(auto nv : calced) { tmp_m = nv.first + next.m; if (tmp_m >=m) { tmp_m -= m; } tmp_c = nv.second + next.c; if (calced[tmp_m] == 0 || calced[tmp_m] > tmp_c) { calced[tmp_m] = tmp_c; to_check.push(job{ m: tmp_m, c: tmp_c, }); } } tmp_m = next.m; tmp_c = next.c; } cout << "0\n"; for (int i = 1 ; i< m ; i++) { ci = calced[i]; if (ci) { cout << ci << "\n"; } else { cout << "-1\n"; } } }
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 <bits/stdc++.h> #define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); #include <iostream> #include <unordered_map> #include <vector> #include <queue> using namespace std; void failure_fine(int m) { cout <<"0\n"; for (int i = 1; i < m; i++) { cout << "-1\n"; } } struct job { int64_t m; int64_t c; }; int main() { FAST uint16_t n, k, m; cin >> n >> k >> m; uint16_t ki; int64_t mi; int64_t ci; vector<unordered_map<int64_t, int64_t>> opts(k); for (int i = 0; i < n; i++) { cin >> ki >> mi >> ci; ki--; if (mi == m) { mi = 0; } if (opts[ki].count(mi) == 0 || opts[ki][mi]>ci) { opts[ki][mi] = ci; } } unordered_map<int64_t, int64_t> calced; calced.reserve(m); int64_t tmp_c; int64_t tmp_m; unordered_map<int64_t, int64_t> tmp; tmp.reserve(m); for (auto v: opts) { tmp.clear(); if (v.size() == 0) { failure_fine(m); return 0; } if (calced.size() == 0) { for (auto nv: v) { calced[nv.first] = nv.second; } continue; } for (auto ov: calced) { for (auto nv: v) { tmp_m = nv.first + ov.first; if (tmp_m >=m) { tmp_m -= m; } tmp_c = nv.second + ov.second; if (tmp[tmp_m] == 0 || tmp[tmp_m] > tmp_c) { tmp[tmp_m] = tmp_c; } } } calced = tmp; } queue<job> to_check; for (auto v: calced) { to_check.push(job{ m: v.first, c: v.second, }); } job next; while (to_check.size()) { next = to_check.front(); to_check.pop(); if (calced[next.m] < next.c) { continue; } for(auto nv : calced) { tmp_m = nv.first + next.m; if (tmp_m >=m) { tmp_m -= m; } tmp_c = nv.second + next.c; if (calced[tmp_m] == 0 || calced[tmp_m] > tmp_c) { calced[tmp_m] = tmp_c; to_check.push(job{ m: tmp_m, c: tmp_c, }); } } tmp_m = next.m; tmp_c = next.c; } cout << "0\n"; for (int i = 1 ; i< m ; i++) { ci = calced[i]; if (ci) { cout << ci << "\n"; } else { cout << "-1\n"; } } } |