#include <bits/stdc++.h> #include <climits> using namespace std; #define pb push_back #define fi first #define sn second typedef long long ll; typedef vector<int> VI; typedef vector<ll> VLL; typedef vector<char> VC; typedef pair<int, int> PI; struct Jelly { int k; int m; ll w; }; struct Event { int m; ll w; Event(int m, ll w) : m(m), w(w) {} }; bool operator<(Event a, Event b) { return a.w < b.w; } // int nxt(int current) { return (current + 1) % 2; } int main() { int n, k, m; cin >> n >> k >> m; vector<vector<Jelly>> J(k); vector<VLL> DP(k, VLL(m, LLONG_MAX)); vector<ll> R(m, LLONG_MAX); for (int i = 0; i < n; i++) { Jelly jelly; cin >> jelly.k >> jelly.m >> jelly.w; jelly.k--; J[jelly.k].push_back(jelly); } for (Jelly j : J[0]) { DP[0][j.m] = min(DP[0][j.m], j.w); } for (int c = 1; c < k; c++) { for (int m0 = 0; m0 < m; m0++) { if (DP[c - 1][m0] == LLONG_MAX) continue; for (Jelly j : J[c]) { int new_m = (m0 + j.m) % m; DP[c][new_m] = min(DP[c][new_m], DP[c - 1][m0] + j.w); } } } priority_queue<Event> Q; int c = k - 1; for (int m0 = 0; m0 < m; m0++) { if (DP[c][m0] < LLONG_MAX) { Q.push(Event(m0, DP[c][m0])); } } R[0] = 0; while (!Q.empty()) { Event e = Q.top(); Q.pop(); if (DP[c][e.m] < e.w) { continue; } R[e.m] = min(R[e.m], e.w); for (int m0 = 0; m0 < m; m0++) { if (DP[c][m0] == LLONG_MAX) continue; int new_m = (m0 + e.m) % m; ll new_w = DP[c][m0] + e.w; if (DP[c][new_m] > new_w) { DP[c][new_m] = new_w; Q.push(Event(new_m, new_w)); } } } for (ll w : R) { if (w == LLONG_MAX) { cout << -1; } else { cout << w; } cout << endl; } 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 | #include <bits/stdc++.h> #include <climits> using namespace std; #define pb push_back #define fi first #define sn second typedef long long ll; typedef vector<int> VI; typedef vector<ll> VLL; typedef vector<char> VC; typedef pair<int, int> PI; struct Jelly { int k; int m; ll w; }; struct Event { int m; ll w; Event(int m, ll w) : m(m), w(w) {} }; bool operator<(Event a, Event b) { return a.w < b.w; } // int nxt(int current) { return (current + 1) % 2; } int main() { int n, k, m; cin >> n >> k >> m; vector<vector<Jelly>> J(k); vector<VLL> DP(k, VLL(m, LLONG_MAX)); vector<ll> R(m, LLONG_MAX); for (int i = 0; i < n; i++) { Jelly jelly; cin >> jelly.k >> jelly.m >> jelly.w; jelly.k--; J[jelly.k].push_back(jelly); } for (Jelly j : J[0]) { DP[0][j.m] = min(DP[0][j.m], j.w); } for (int c = 1; c < k; c++) { for (int m0 = 0; m0 < m; m0++) { if (DP[c - 1][m0] == LLONG_MAX) continue; for (Jelly j : J[c]) { int new_m = (m0 + j.m) % m; DP[c][new_m] = min(DP[c][new_m], DP[c - 1][m0] + j.w); } } } priority_queue<Event> Q; int c = k - 1; for (int m0 = 0; m0 < m; m0++) { if (DP[c][m0] < LLONG_MAX) { Q.push(Event(m0, DP[c][m0])); } } R[0] = 0; while (!Q.empty()) { Event e = Q.top(); Q.pop(); if (DP[c][e.m] < e.w) { continue; } R[e.m] = min(R[e.m], e.w); for (int m0 = 0; m0 < m; m0++) { if (DP[c][m0] == LLONG_MAX) continue; int new_m = (m0 + e.m) % m; ll new_w = DP[c][m0] + e.w; if (DP[c][new_m] > new_w) { DP[c][new_m] = new_w; Q.push(Event(new_m, new_w)); } } } for (ll w : R) { if (w == LLONG_MAX) { cout << -1; } else { cout << w; } cout << endl; } return 0; } |