// #pragma GCC optimize("O3") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native") #include <bits/stdc++.h> using namespace std; #define PB emplace_back #define int long long #define ll long long #define vi vector<int> #define siz(a) ((int) ((a).size())) #define rep(i, a, b) for (int i = (a); i <= (b); ++i) #define per(i, a, b) for (int i = (a); i >= (b); --i) void print(vi n) { rep(i, 0, siz(n) - 1) cerr << n[i] << " \n"[i == siz(n) - 1]; } const int N = 7000; int a, b, c, f[N + 5], g[N + 5]; vector<pair<int, int>> st[N + 5]; bool vis[N + 5]; int mod(int n) {return n >= c ? n - c : n;} void ck(int &n, int m) {n = min(n, m);} signed main() { // freopen(".in", "r", stdin); // freopen(".out", "w", stdout); ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin >> a >> b >> c; int x, y, z; rep(i, 1, a) { cin >> x >> y >> z; st[x].PB(y, z); } memset(f, 0x3f, sizeof(f)); f[0] = 0; rep(i, 1, b) { memset(g, 0x3f, sizeof(g)); for(auto [x, y] : st[i]) rep(i, 0, c - 1) ck(g[mod(i + x)], f[i] + y); memcpy(f, g, sizeof(g)); } memset(g, 0x3f, sizeof(g)); const int inf = g[0]; g[0] = 0; rep(i, 1, c) { int u = -1, mn = inf; rep(j, 0, c - 1) if(!vis[j] && g[j] < mn) mn = g[j], u = j; if(u == -1) break; vis[u] = 1; rep(j, 0, c - 1) ck(g[mod(u + j)], mn + f[j]); } rep(i, 0, c - 1) cout << (g[i] < inf ? g[i] : -1) << '\n'; return cerr << endl << 1.0 * clock() / CLOCKS_PER_SEC << endl, 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 | // #pragma GCC optimize("O3") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native") #include <bits/stdc++.h> using namespace std; #define PB emplace_back #define int long long #define ll long long #define vi vector<int> #define siz(a) ((int) ((a).size())) #define rep(i, a, b) for (int i = (a); i <= (b); ++i) #define per(i, a, b) for (int i = (a); i >= (b); --i) void print(vi n) { rep(i, 0, siz(n) - 1) cerr << n[i] << " \n"[i == siz(n) - 1]; } const int N = 7000; int a, b, c, f[N + 5], g[N + 5]; vector<pair<int, int>> st[N + 5]; bool vis[N + 5]; int mod(int n) {return n >= c ? n - c : n;} void ck(int &n, int m) {n = min(n, m);} signed main() { // freopen(".in", "r", stdin); // freopen(".out", "w", stdout); ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin >> a >> b >> c; int x, y, z; rep(i, 1, a) { cin >> x >> y >> z; st[x].PB(y, z); } memset(f, 0x3f, sizeof(f)); f[0] = 0; rep(i, 1, b) { memset(g, 0x3f, sizeof(g)); for(auto [x, y] : st[i]) rep(i, 0, c - 1) ck(g[mod(i + x)], f[i] + y); memcpy(f, g, sizeof(g)); } memset(g, 0x3f, sizeof(g)); const int inf = g[0]; g[0] = 0; rep(i, 1, c) { int u = -1, mn = inf; rep(j, 0, c - 1) if(!vis[j] && g[j] < mn) mn = g[j], u = j; if(u == -1) break; vis[u] = 1; rep(j, 0, c - 1) ck(g[mod(u + j)], mn + f[j]); } rep(i, 0, c - 1) cout << (g[i] < inf ? g[i] : -1) << '\n'; return cerr << endl << 1.0 * clock() / CLOCKS_PER_SEC << endl, 0; } |