#include <bits/stdc++.h> #ifdef dbg #define D(...) fprintf(stderr, __VA_ARGS__) #define DD(...) D("[Debug] "#__VA_ARGS__ " = "), \ debug_helper::debug(__VA_ARGS__), D("\n") #include "C:\Users\wsyear\Desktop\OI\templates\debug.hpp" #else #define D(...) ((void)0) #define DD(...) ((void)0) #endif #define rep(i, j, k) for (int i = (j); i <= (k); ++i) #define per(i, j, k) for (int i = (j); i >= (k); --i) #define SZ(v) int((v).size()) #define ALL(v) (v).begin(),(v).end() #define fi first #define se second using ll = long long; using pii = std::pair<int, int>; using pll = std::pair<ll, ll>; template<class T> void chkmn(T &x, T y) { if (y < x) x = y; } template<class T> void chkmx(T &x, T y) { if (y > x) x = y; } using namespace std; const int maxn = 7010; const ll inf = 1e18; int n, m, k; ll f[maxn], g[maxn], dis[maxn]; vector<pii> a[maxn]; priority_queue<pll, vector<pll>, greater<pll>> q; int main() { cin.tie(nullptr) -> ios::sync_with_stdio(false); cin >> n >> k >> m; rep (i, 1, n) { int col, x, y; cin >> col >> x >> y; a[col].emplace_back(x, y); } rep (i, 0, m - 1) f[i] = inf; f[0] = 0; rep (i, 1, k) { rep (j, 0, m - 1) g[j] = f[j], f[j] = inf; for (auto [x, y] : a[i]) { rep (j, 0, m - 1) chkmn(f[(j + x) % m], g[j] + y); } } rep (i, 0, m - 1) dis[i] = inf; dis[0] = 0, q.emplace(0, 0); while (!q.empty()) { auto [cd, u] = q.top(); q.pop(); if (cd != dis[u]) continue; rep (i, 0, m - 1) if (cd + f[i] < dis[(u + i) % m]) { dis[(u + i) % m] = cd + f[i]; q.emplace(cd + f[i], (u + i) % m); } } rep (i, 0, m - 1) cout << (dis[i] == inf ? -1 : dis[i]) << '\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 | #include <bits/stdc++.h> #ifdef dbg #define D(...) fprintf(stderr, __VA_ARGS__) #define DD(...) D("[Debug] "#__VA_ARGS__ " = "), \ debug_helper::debug(__VA_ARGS__), D("\n") #include "C:\Users\wsyear\Desktop\OI\templates\debug.hpp" #else #define D(...) ((void)0) #define DD(...) ((void)0) #endif #define rep(i, j, k) for (int i = (j); i <= (k); ++i) #define per(i, j, k) for (int i = (j); i >= (k); --i) #define SZ(v) int((v).size()) #define ALL(v) (v).begin(),(v).end() #define fi first #define se second using ll = long long; using pii = std::pair<int, int>; using pll = std::pair<ll, ll>; template<class T> void chkmn(T &x, T y) { if (y < x) x = y; } template<class T> void chkmx(T &x, T y) { if (y > x) x = y; } using namespace std; const int maxn = 7010; const ll inf = 1e18; int n, m, k; ll f[maxn], g[maxn], dis[maxn]; vector<pii> a[maxn]; priority_queue<pll, vector<pll>, greater<pll>> q; int main() { cin.tie(nullptr) -> ios::sync_with_stdio(false); cin >> n >> k >> m; rep (i, 1, n) { int col, x, y; cin >> col >> x >> y; a[col].emplace_back(x, y); } rep (i, 0, m - 1) f[i] = inf; f[0] = 0; rep (i, 1, k) { rep (j, 0, m - 1) g[j] = f[j], f[j] = inf; for (auto [x, y] : a[i]) { rep (j, 0, m - 1) chkmn(f[(j + x) % m], g[j] + y); } } rep (i, 0, m - 1) dis[i] = inf; dis[0] = 0, q.emplace(0, 0); while (!q.empty()) { auto [cd, u] = q.top(); q.pop(); if (cd != dis[u]) continue; rep (i, 0, m - 1) if (cd + f[i] < dis[(u + i) % m]) { dis[(u + i) % m] = cd + f[i]; q.emplace(cd + f[i], (u + i) % m); } } rep (i, 0, m - 1) cout << (dis[i] == inf ? -1 : dis[i]) << '\n'; } |