#include <bits/stdc++.h> using namespace std; using ll = long long; #ifdef LOCAL #include "debug.h" #else #define debug(...) 2137 #endif bool chmin(ll& x, ll y) { if (y == -1) return false; return (x == -1 || y < x) ? x = y, true : false; } const int N = 7070; int n, c, m; array<int, 3> a[N]; ll dp[2][N]; bool vis[N]; int dod(int x, int y) { return x + y >= m ? x + y - m : x + y; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> c >> m; for (int i = 0; i < n; i++) { for (int j = 0; j < 3; j++) { cin >> a[i][j]; } } sort(a, a + n); for (int i = 0; i < m; i++) { dp[0][i] = dp[1][i] = -1; } dp[0][0] = 0; int mc = 1; for (int i = 0, j = 0; i < n; i = j) { while (j < n && a[i][0] == a[j][0]) { for (int k = 0; k < m; k++) { if (dp[0][k] != -1) { chmin(dp[1][dod(k, a[j][1])], dp[0][k] + a[j][2]); } } j++; } if (a[i][0] == mc) { mc++; } for (int k = 0; k < m; k++) { dp[0][k] = exchange(dp[1][k], -1); } } dp[0][0] = dp[1][0] = 0; if (mc == c + 1) { while (true) { int u = -1; ll d = -1; for (int i = 0; i < m; i++) { if (!vis[i] && chmin(d, dp[1][i])) { u = i; } } if (u == -1) { break; } vis[u] = true; for (int i = 0; i < m; i++) { if (dp[0][i] != -1) { chmin(dp[1][dod(u, i)], d + dp[0][i]); } } } } for (int i = 0; i < m; i++) { cout << dp[1][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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | #include <bits/stdc++.h> using namespace std; using ll = long long; #ifdef LOCAL #include "debug.h" #else #define debug(...) 2137 #endif bool chmin(ll& x, ll y) { if (y == -1) return false; return (x == -1 || y < x) ? x = y, true : false; } const int N = 7070; int n, c, m; array<int, 3> a[N]; ll dp[2][N]; bool vis[N]; int dod(int x, int y) { return x + y >= m ? x + y - m : x + y; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> c >> m; for (int i = 0; i < n; i++) { for (int j = 0; j < 3; j++) { cin >> a[i][j]; } } sort(a, a + n); for (int i = 0; i < m; i++) { dp[0][i] = dp[1][i] = -1; } dp[0][0] = 0; int mc = 1; for (int i = 0, j = 0; i < n; i = j) { while (j < n && a[i][0] == a[j][0]) { for (int k = 0; k < m; k++) { if (dp[0][k] != -1) { chmin(dp[1][dod(k, a[j][1])], dp[0][k] + a[j][2]); } } j++; } if (a[i][0] == mc) { mc++; } for (int k = 0; k < m; k++) { dp[0][k] = exchange(dp[1][k], -1); } } dp[0][0] = dp[1][0] = 0; if (mc == c + 1) { while (true) { int u = -1; ll d = -1; for (int i = 0; i < m; i++) { if (!vis[i] && chmin(d, dp[1][i])) { u = i; } } if (u == -1) { break; } vis[u] = true; for (int i = 0; i < m; i++) { if (dp[0][i] != -1) { chmin(dp[1][dod(u, i)], d + dp[0][i]); } } } } for (int i = 0; i < m; i++) { cout << dp[1][i] << '\n'; } } |