#include<bits/stdc++.h> using namespace std; #define int int64_t #define pb push_back #define pii pair<int, int> #define st first #define nd second #define sz(x) (int)(x).size() #define all(x) (x).begin(), (x).end() const int MAXN = 7'009; const int INF = 1e18; vector<pii> V[MAXN]; int dp[2][MAXN]; int ans[MAXN]; int f(int a, int b, int c) { return a+b>=c?a+b-c:a+b; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, k, m; cin >> n >> k >> m; for(int i=0;i<n;i++) { int a, b, c; cin >> a >> b >> c; V[a].pb({b, c}); } for(int i=0;i<m;i++) dp[0][i] = dp[1][i] = INF; dp[0][0] = 0; for(int i=1;i<=k;i++) { for(auto [w, c]:V[i]) { for(int j=0;j<m;j++) { dp[1][f(j, w, m)] = min(dp[1][f(j, w, m)], dp[0][j]+c); } } for(int i=0;i<m;i++) { dp[0][i] = dp[1][i]; dp[1][i] = INF; } } for(int i=0;i<m;i++) { int c = 0; int val = 0; for(int j=0;j<=m;j++) { dp[0][val] = min(dp[0][val], c); c = min(c+dp[0][i], INF); val = f(val, i, m); } } for(int i=0;i<m;i++) ans[i] = INF; ans[0] = 0; for(int i=0;i<m;i++) { for(int j=0;j<m;j++) { ans[f(i, j, m)] = min(ans[f(i, j, m)], ans[j]+dp[0][i]); } } for(int i=0;i<m;i++) { if(ans[i]==INF) cout << "-1\n"; else cout << ans[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 | #include<bits/stdc++.h> using namespace std; #define int int64_t #define pb push_back #define pii pair<int, int> #define st first #define nd second #define sz(x) (int)(x).size() #define all(x) (x).begin(), (x).end() const int MAXN = 7'009; const int INF = 1e18; vector<pii> V[MAXN]; int dp[2][MAXN]; int ans[MAXN]; int f(int a, int b, int c) { return a+b>=c?a+b-c:a+b; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, k, m; cin >> n >> k >> m; for(int i=0;i<n;i++) { int a, b, c; cin >> a >> b >> c; V[a].pb({b, c}); } for(int i=0;i<m;i++) dp[0][i] = dp[1][i] = INF; dp[0][0] = 0; for(int i=1;i<=k;i++) { for(auto [w, c]:V[i]) { for(int j=0;j<m;j++) { dp[1][f(j, w, m)] = min(dp[1][f(j, w, m)], dp[0][j]+c); } } for(int i=0;i<m;i++) { dp[0][i] = dp[1][i]; dp[1][i] = INF; } } for(int i=0;i<m;i++) { int c = 0; int val = 0; for(int j=0;j<=m;j++) { dp[0][val] = min(dp[0][val], c); c = min(c+dp[0][i], INF); val = f(val, i, m); } } for(int i=0;i<m;i++) ans[i] = INF; ans[0] = 0; for(int i=0;i<m;i++) { for(int j=0;j<m;j++) { ans[f(i, j, m)] = min(ans[f(i, j, m)], ans[j]+dp[0][i]); } } for(int i=0;i<m;i++) { if(ans[i]==INF) cout << "-1\n"; else cout << ans[i] << "\n"; } } |