#include <iostream> #include <vector> #include <algorithm> #include <unordered_map> #include <unordered_set> #include <climits> #include <set> #include <queue> #include <chrono> using namespace std; void dfs(int v, vector<unordered_set<int>> &edges, vector<bool> &vis, vector<int> &topo) { if (vis[v]) return; vis[v] = true; for (auto &u : edges[v]) { if (vis[u]) continue; if (edges[0].find(u) == edges[0].end()) { topo.push_back(u); } dfs(u, edges, vis, topo); } } long long dp(vector<int> d, int h, int w) { // cout << h << ", " << w << endl; if (h == 0 || w == 0) return 0; if (d.empty()) return -1; int di = d.back(); int a = h / di; int b = w / di; long long cnt = (long long) a * (long long) b; d.pop_back(); long long ret1 = dp(d, h, w % di); long long ret2 = dp(d, h % di, w / di * di); if (ret1 == -1 || ret2 == -1) return -1; return ret1 + ret2 + cnt; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); auto start = std::chrono::high_resolution_clock::now(); int n, k, m; cin >> n >> k >> m; vector<vector<pair<int, int>>> v(k); for (int i = 0; i < n; ++i) { int ki, mi, ci; cin >> ki >> mi >> ci; v[ki - 1].push_back({mi % m, ci}); } vector<long long> delta(m, LLONG_MAX); delta[0] = 0; for (int i = 0; i < k; ++i) { vector<long long> delta_new(m, LLONG_MAX); for (auto &[mi, ci] : v[i]) { for (int j = 0; j < m; ++j) { if (delta[j] == LLONG_MAX) continue; delta_new[(j + mi) % m] = min(delta_new[(j + mi) % m], delta[j] + ci); } } delta = delta_new; } int nonempty = 0; for (auto v : delta) if (v != LLONG_MAX) ++nonempty; // vector<vector<pair<long long, int>>> edges(m); // for (int i = 0; i < m; ++i) { // edges[i].reserve(nonempty); // for (int d = 0; d < m; ++d) { // if (delta[d] == LLONG_MAX) continue; // int j = i + d; // if (j >= m) { // j -= m; // } // edges[i].push_back({delta[d], j}); // } // } vector<long long> dist(m, LLONG_MAX); dist[0] = 0; set<pair<long long, int>> se; se.insert({0, 0}); while (!se.empty()) { auto it = se.begin(); auto [d, u] = *it; se.erase(it); for (int d = 0; d < m; ++d) { if (delta[d] == LLONG_MAX) continue; int v = u + d; if (v >= m) { v -= m; } auto w = delta[d]; if (dist[u] + w < dist[v]) { auto it1 = se.find({dist[v], v}); if (it1 != se.end()) { se.erase(it1); } dist[v] = dist[u] + w; se.insert({dist[v], v}); } } } for (auto v : dist) { if (v == LLONG_MAX) v = -1; cout << v << "\n"; } auto stop = std::chrono::high_resolution_clock::now(); auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(stop - start); cerr << "Time taken by while loop: " << duration.count() << " ms" << endl; }
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 146 147 148 149 | #include <iostream> #include <vector> #include <algorithm> #include <unordered_map> #include <unordered_set> #include <climits> #include <set> #include <queue> #include <chrono> using namespace std; void dfs(int v, vector<unordered_set<int>> &edges, vector<bool> &vis, vector<int> &topo) { if (vis[v]) return; vis[v] = true; for (auto &u : edges[v]) { if (vis[u]) continue; if (edges[0].find(u) == edges[0].end()) { topo.push_back(u); } dfs(u, edges, vis, topo); } } long long dp(vector<int> d, int h, int w) { // cout << h << ", " << w << endl; if (h == 0 || w == 0) return 0; if (d.empty()) return -1; int di = d.back(); int a = h / di; int b = w / di; long long cnt = (long long) a * (long long) b; d.pop_back(); long long ret1 = dp(d, h, w % di); long long ret2 = dp(d, h % di, w / di * di); if (ret1 == -1 || ret2 == -1) return -1; return ret1 + ret2 + cnt; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); auto start = std::chrono::high_resolution_clock::now(); int n, k, m; cin >> n >> k >> m; vector<vector<pair<int, int>>> v(k); for (int i = 0; i < n; ++i) { int ki, mi, ci; cin >> ki >> mi >> ci; v[ki - 1].push_back({mi % m, ci}); } vector<long long> delta(m, LLONG_MAX); delta[0] = 0; for (int i = 0; i < k; ++i) { vector<long long> delta_new(m, LLONG_MAX); for (auto &[mi, ci] : v[i]) { for (int j = 0; j < m; ++j) { if (delta[j] == LLONG_MAX) continue; delta_new[(j + mi) % m] = min(delta_new[(j + mi) % m], delta[j] + ci); } } delta = delta_new; } int nonempty = 0; for (auto v : delta) if (v != LLONG_MAX) ++nonempty; // vector<vector<pair<long long, int>>> edges(m); // for (int i = 0; i < m; ++i) { // edges[i].reserve(nonempty); // for (int d = 0; d < m; ++d) { // if (delta[d] == LLONG_MAX) continue; // int j = i + d; // if (j >= m) { // j -= m; // } // edges[i].push_back({delta[d], j}); // } // } vector<long long> dist(m, LLONG_MAX); dist[0] = 0; set<pair<long long, int>> se; se.insert({0, 0}); while (!se.empty()) { auto it = se.begin(); auto [d, u] = *it; se.erase(it); for (int d = 0; d < m; ++d) { if (delta[d] == LLONG_MAX) continue; int v = u + d; if (v >= m) { v -= m; } auto w = delta[d]; if (dist[u] + w < dist[v]) { auto it1 = se.find({dist[v], v}); if (it1 != se.end()) { se.erase(it1); } dist[v] = dist[u] + w; se.insert({dist[v], v}); } } } for (auto v : dist) { if (v == LLONG_MAX) v = -1; cout << v << "\n"; } auto stop = std::chrono::high_resolution_clock::now(); auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(stop - start); cerr << "Time taken by while loop: " << duration.count() << " ms" << endl; } |