#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, n) for (int i = 0; i < n; i++)
#define f first
#define s second
#define pb push_back
#define all(s) s.begin(), s.end()
#define sz(s) (int)s.size()
using vi = vector<int>;
using ll = long long;
using pll = pair<ll, ll>;
using vll = vector<ll>;
using ii = pair<int, int>;
const ll BIG_INF = 1e18;
void solve()
{
int n, k, m;
cin >> n >> k >> m;
vector<vector<pair<int, int>>> colors(k);
FOR(i, n)
{
int col, w, cost;
cin >> col >> w >> cost;
col--;
colors[col].pb({w, cost});
}
vll dp(m, -1);
dp[0] = 0;
FOR(col, k)
{
vll nowe(m, -1);
for (auto [w, cost] : colors[col])
{
FOR(w_start, m)
{
if (dp[w_start] == -1)
{
continue;
}
int new_w = w + w_start;
if (new_w >= m)
{
new_w -= m;
}
if (nowe[new_w] == -1 or nowe[new_w] > dp[w_start] + cost)
{
nowe[new_w] = dp[w_start] + cost;
}
}
}
swap(dp, nowe);
}
vll dist(m, BIG_INF);
priority_queue<pll, vector<pll>, greater<pll> > pq;
dist[0] = 0;
pq.push({0, 0});
while (sz(pq))
{
auto [cost, w] = pq.top();
pq.pop();
if (dist[w] != cost)
{
continue;
}
FOR (diff, m)
{
if (dp[diff] == -1)
{
continue;
}
ll new_cost = cost + dp[diff];
int new_w = w + diff;
if (new_w >= m)
{
new_w -= m;
}
if (new_cost < dist[new_w])
{
dist[new_w] = new_cost;
pq.push({new_cost, new_w});
}
}
}
FOR (i, m)
{
cout << (dist[i] == BIG_INF ? -1 : dist[i]) << '\n';
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int tests = 1;
// cin >> tests;
for (int test = 1; test <= tests; test++)
{
solve();
}
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 113 114 115 116 117 118 119 120 121 122 | #pragma GCC optimize("O3") #include <bits/stdc++.h> using namespace std; #define FOR(i, n) for (int i = 0; i < n; i++) #define f first #define s second #define pb push_back #define all(s) s.begin(), s.end() #define sz(s) (int)s.size() using vi = vector<int>; using ll = long long; using pll = pair<ll, ll>; using vll = vector<ll>; using ii = pair<int, int>; const ll BIG_INF = 1e18; void solve() { int n, k, m; cin >> n >> k >> m; vector<vector<pair<int, int>>> colors(k); FOR(i, n) { int col, w, cost; cin >> col >> w >> cost; col--; colors[col].pb({w, cost}); } vll dp(m, -1); dp[0] = 0; FOR(col, k) { vll nowe(m, -1); for (auto [w, cost] : colors[col]) { FOR(w_start, m) { if (dp[w_start] == -1) { continue; } int new_w = w + w_start; if (new_w >= m) { new_w -= m; } if (nowe[new_w] == -1 or nowe[new_w] > dp[w_start] + cost) { nowe[new_w] = dp[w_start] + cost; } } } swap(dp, nowe); } vll dist(m, BIG_INF); priority_queue<pll, vector<pll>, greater<pll> > pq; dist[0] = 0; pq.push({0, 0}); while (sz(pq)) { auto [cost, w] = pq.top(); pq.pop(); if (dist[w] != cost) { continue; } FOR (diff, m) { if (dp[diff] == -1) { continue; } ll new_cost = cost + dp[diff]; int new_w = w + diff; if (new_w >= m) { new_w -= m; } if (new_cost < dist[new_w]) { dist[new_w] = new_cost; pq.push({new_cost, new_w}); } } } FOR (i, m) { cout << (dist[i] == BIG_INF ? -1 : dist[i]) << '\n'; } } int main() { ios::sync_with_stdio(0); cin.tie(0); int tests = 1; // cin >> tests; for (int test = 1; test <= tests; test++) { solve(); } return 0; } |
English