// Karol Kosinski 2024 #include <bits/stdc++.h> #define FOR(i,a,b) for(int i=(a),_b=(b);i<_b;++i) #define FR_(i,a,b) for(int i=(a),_b=(b);i<=_b;++i) #define FD_(i,b,a) for(int i=(b),_a=(a);i>=_a;--i) #define ALL(c) (c).begin(),(c).end() #define SIZE(c) int((c).size()) #define X first #define Y second #define endl '\n' #define NAM(x) #x,'=',x using namespace std; using LL = long long; using ULL = unsigned long long; using PII = pair<int, int>; using TIII = tuple<int, int, int>; template<class...T> void _cout(T...a){(cout<<...<<a);} #ifndef ENABLE_DEBUG #define DEB(k,p,f,x...) #else #define DEB(k,p,f,x...) {if(k)_cout("------",setw(4),__LINE__," : ",__FUNCTION__,endl);if(p)f(x);} #endif #define DEBF(f,x...) DEB(1,1,f,x) #define DEBL DEBF(void,0) #define DEBC(p,x...) DEB(0,p,_cout,x) #define DEBUG(x...) DEBC(1,x) constexpr int NX = 7'005; constexpr LL INFTY = LL(1e18) + 1; namespace Dijkstra { using PT = pair<LL, int>; int n; LL D[NX]; vector<PT> G[NX]; void add(int u, int v, LL w) { G[u].emplace_back(w, v); } void clear() { FOR(i,0,n) G[i].clear(); } void init(int ng, int root) { n = ng; priority_queue<PT, vector<PT>, greater<PT>> Q; FOR(i,0,n) D[i] = INFTY; Q.emplace( D[root] = 0, root ); while ( not Q.empty() ) { auto [du, u] = Q.top(); Q.pop(); if ( du != D[u] ) continue; for ( auto& [w, v] : G[u] ) if ( D[v] > du + w ) { D[v] = du + w; Q.emplace( D[v], v ); } } } }; struct Jelly { int jk, jm; LL jc; }; bool operator < (const Jelly& a, const Jelly& b) { return a.jk < b.jk; } Jelly J[NX]; LL E[2][NX], *PR, *CR; void print_row(int jk, int m, LL* Row) { cout << "[" << jk << "] : "; FOR(i,0,m) cout << Row[i] << " "; cout << endl; } bool compute_edges(int n, int k, int m) { int colors = 0, last = 0; PR = E[0], CR = E[1]; FOR(i,0,m) CR[i] = INFTY; CR[0] = 0; DEBF( print_row, 0, m, CR ); FOR(i,0,n) { auto& [jk, jm, jc] = J[i]; if ( jk != last) { swap(PR, CR); FOR(j,0,m) CR[j] = INFTY; ++ colors; } FOR(j,0,m) { auto& e = CR[ ( j + jm ) % m ]; e = min( e, PR[j] + jc ); } DEBF( print_row, jk, m, CR ); last = jk; } DEBUG( NAM(colors), " vs ", NAM(k), endl ); return colors == k; } void run_dijkstra(int m) { DEBL; FOR(j,1,m) { if ( CR[j] == INFTY ) continue; FOR(i,0,m) Dijkstra::add( i, (i + j) % m, CR[j] ); } Dijkstra::init(m, 0); } void testcase() { int n, k, m; cin >> n >> k >> m; if ( m == 1 ) { cout << 0 << endl; return; } FOR(i,0,n) { int a, b; LL c; cin >> a >> b >> c; J[i] = Jelly{ a, b, c }; } sort( J, J + n ); if ( compute_edges(n, k, m) ) { run_dijkstra(m); FOR(i,0,m) { LL d = Dijkstra::D[i]; cout << ( ( d == INFTY ) ? -1 : d ) << endl; } } else { cout << 0 << endl; FOR(i,1,m) cout << -1 << endl; } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); DEBUG( NAM(INFTY), endl ); testcase(); 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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | // Karol Kosinski 2024 #include <bits/stdc++.h> #define FOR(i,a,b) for(int i=(a),_b=(b);i<_b;++i) #define FR_(i,a,b) for(int i=(a),_b=(b);i<=_b;++i) #define FD_(i,b,a) for(int i=(b),_a=(a);i>=_a;--i) #define ALL(c) (c).begin(),(c).end() #define SIZE(c) int((c).size()) #define X first #define Y second #define endl '\n' #define NAM(x) #x,'=',x using namespace std; using LL = long long; using ULL = unsigned long long; using PII = pair<int, int>; using TIII = tuple<int, int, int>; template<class...T> void _cout(T...a){(cout<<...<<a);} #ifndef ENABLE_DEBUG #define DEB(k,p,f,x...) #else #define DEB(k,p,f,x...) {if(k)_cout("------",setw(4),__LINE__," : ",__FUNCTION__,endl);if(p)f(x);} #endif #define DEBF(f,x...) DEB(1,1,f,x) #define DEBL DEBF(void,0) #define DEBC(p,x...) DEB(0,p,_cout,x) #define DEBUG(x...) DEBC(1,x) constexpr int NX = 7'005; constexpr LL INFTY = LL(1e18) + 1; namespace Dijkstra { using PT = pair<LL, int>; int n; LL D[NX]; vector<PT> G[NX]; void add(int u, int v, LL w) { G[u].emplace_back(w, v); } void clear() { FOR(i,0,n) G[i].clear(); } void init(int ng, int root) { n = ng; priority_queue<PT, vector<PT>, greater<PT>> Q; FOR(i,0,n) D[i] = INFTY; Q.emplace( D[root] = 0, root ); while ( not Q.empty() ) { auto [du, u] = Q.top(); Q.pop(); if ( du != D[u] ) continue; for ( auto& [w, v] : G[u] ) if ( D[v] > du + w ) { D[v] = du + w; Q.emplace( D[v], v ); } } } }; struct Jelly { int jk, jm; LL jc; }; bool operator < (const Jelly& a, const Jelly& b) { return a.jk < b.jk; } Jelly J[NX]; LL E[2][NX], *PR, *CR; void print_row(int jk, int m, LL* Row) { cout << "[" << jk << "] : "; FOR(i,0,m) cout << Row[i] << " "; cout << endl; } bool compute_edges(int n, int k, int m) { int colors = 0, last = 0; PR = E[0], CR = E[1]; FOR(i,0,m) CR[i] = INFTY; CR[0] = 0; DEBF( print_row, 0, m, CR ); FOR(i,0,n) { auto& [jk, jm, jc] = J[i]; if ( jk != last) { swap(PR, CR); FOR(j,0,m) CR[j] = INFTY; ++ colors; } FOR(j,0,m) { auto& e = CR[ ( j + jm ) % m ]; e = min( e, PR[j] + jc ); } DEBF( print_row, jk, m, CR ); last = jk; } DEBUG( NAM(colors), " vs ", NAM(k), endl ); return colors == k; } void run_dijkstra(int m) { DEBL; FOR(j,1,m) { if ( CR[j] == INFTY ) continue; FOR(i,0,m) Dijkstra::add( i, (i + j) % m, CR[j] ); } Dijkstra::init(m, 0); } void testcase() { int n, k, m; cin >> n >> k >> m; if ( m == 1 ) { cout << 0 << endl; return; } FOR(i,0,n) { int a, b; LL c; cin >> a >> b >> c; J[i] = Jelly{ a, b, c }; } sort( J, J + n ); if ( compute_edges(n, k, m) ) { run_dijkstra(m); FOR(i,0,m) { LL d = Dijkstra::D[i]; cout << ( ( d == INFTY ) ? -1 : d ) << endl; } } else { cout << 0 << endl; FOR(i,1,m) cout << -1 << endl; } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); DEBUG( NAM(INFTY), endl ); testcase(); return 0; } |