#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; using ld = long double; using vi = vector<int>; using vll = vector<ll>; using vii = vector<pii>; const int N = 7003; ll min_koszt[2][N]; const ll inf = 1'000'000'000'000'000'000LL; vector<pii> zelki[N]; bool vis[N]; priority_queue<pll, vector<pll>, greater<pll>> q; ll dists[N]; void dijkstra(const int m, ll koszt[]) { int s = 0; q.push({0, 0}); for (int i = 1; i < m; i++) { dists[i] = inf; } while (!q.empty()) { auto [_, v] = q.top(); q.pop(); if (vis[v]) { continue; } vis[v] = true; for (int i = 0; i < m; i++) { int idx = (v + i) % m; if (dists[idx] > dists[v] + koszt[i]) { dists[idx] = dists[v] + koszt[i]; q.push({dists[idx], idx}); } } } } int main() { ios_base::sync_with_stdio(false); cin.tie(); int n, k, m1; cin >> n >> k >> m1; const int m = m1; for (int i = 0; i < m; i++) { min_koszt[0][i] = inf; min_koszt[1][i] = inf; } min_koszt[0][0] = 0; for (int i = 0; i < n; i++) { int k2, masa, c; cin >> k2 >> masa >> c; masa %= m; zelki[k2].push_back({masa, c}); } int cur = 0; for (int kolor = 1; kolor <= k; kolor++) { if (zelki[kolor].size() == 0) { cout << 0 << "\n"; for (int i = 1; i < m; i++) { cout << -1 << "\n"; } return 0; } for (auto [masa, c] : zelki[kolor]) { for (int i = 0; i < m; i++) { int idx = (i + masa) % m; min_koszt[cur ^ 1][idx] = min(min_koszt[cur ^ 1][idx], min_koszt[cur][i] + c); } } for (int i = 0; i < m; i++) { min_koszt[cur][i] = inf; } cur ^= 1; // cout << kolor << ":\n"; // for (int i = 0; i < m; i++) { // cout << min_koszt[cur][i] << " "; // } // cout << "\n"; } // for (int i = 0; i < m; i++) { // cout << min_koszt[cur][i] << " "; // } // cout << "\n"; dijkstra(m, min_koszt[cur]); for (int i = 0; i < m; i++) { cout << (vis[i] ? dists[i] : -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 | #include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; using ld = long double; using vi = vector<int>; using vll = vector<ll>; using vii = vector<pii>; const int N = 7003; ll min_koszt[2][N]; const ll inf = 1'000'000'000'000'000'000LL; vector<pii> zelki[N]; bool vis[N]; priority_queue<pll, vector<pll>, greater<pll>> q; ll dists[N]; void dijkstra(const int m, ll koszt[]) { int s = 0; q.push({0, 0}); for (int i = 1; i < m; i++) { dists[i] = inf; } while (!q.empty()) { auto [_, v] = q.top(); q.pop(); if (vis[v]) { continue; } vis[v] = true; for (int i = 0; i < m; i++) { int idx = (v + i) % m; if (dists[idx] > dists[v] + koszt[i]) { dists[idx] = dists[v] + koszt[i]; q.push({dists[idx], idx}); } } } } int main() { ios_base::sync_with_stdio(false); cin.tie(); int n, k, m1; cin >> n >> k >> m1; const int m = m1; for (int i = 0; i < m; i++) { min_koszt[0][i] = inf; min_koszt[1][i] = inf; } min_koszt[0][0] = 0; for (int i = 0; i < n; i++) { int k2, masa, c; cin >> k2 >> masa >> c; masa %= m; zelki[k2].push_back({masa, c}); } int cur = 0; for (int kolor = 1; kolor <= k; kolor++) { if (zelki[kolor].size() == 0) { cout << 0 << "\n"; for (int i = 1; i < m; i++) { cout << -1 << "\n"; } return 0; } for (auto [masa, c] : zelki[kolor]) { for (int i = 0; i < m; i++) { int idx = (i + masa) % m; min_koszt[cur ^ 1][idx] = min(min_koszt[cur ^ 1][idx], min_koszt[cur][i] + c); } } for (int i = 0; i < m; i++) { min_koszt[cur][i] = inf; } cur ^= 1; // cout << kolor << ":\n"; // for (int i = 0; i < m; i++) { // cout << min_koszt[cur][i] << " "; // } // cout << "\n"; } // for (int i = 0; i < m; i++) { // cout << min_koszt[cur][i] << " "; // } // cout << "\n"; dijkstra(m, min_koszt[cur]); for (int i = 0; i < m; i++) { cout << (vis[i] ? dists[i] : -1) << "\n"; } } |