#include<cstdio> #include<algorithm> #include<utility> #include<string> #include<vector> #include<map> #include<set> #include<queue> #include<stack> #include<cmath> #include<iostream> // macros #define FORE(c, a, b) for(int c=a; c<= int(b); ++c) #define FORD(c, a, b) for(int c=a; c>= int(b); --c) #define FORIT(it, cont, cont_t) for(cont_t::iterator it = cont.begin(); it != cont.end(); ++it) #define REP(i, n) for(int i = 0; i < (n); ++i) #define ALL(a) a.begin(), a.end() #define PR(n) printf("%d ", (int) (n)) #define PRL(n) printf("%lld\n", (ll) (n)) #define SC(n) scanf("%d", &n); #define SC2(n, m) scanf("%d%d", &n, &m); #define xx first #define yy second #define pb push_back #define mp make_pair #define itr iterator #define dbg if(0) #define prd dbg printf using namespace std; typedef long long ll; typedef long double ld; typedef unsigned int uint; typedef vector<int> vi; typedef vector<ll> vll; typedef set<int> si; typedef map<int, int> mi; typedef pair<int, int> pi; typedef vector<pi> vii; typedef vector<vi> vvi; const int NMAX = 7007; int n, m, k; ll t[NMAX][NMAX], multi[NMAX][NMAX]; vii zel[NMAX]; vll merge(vll A) { vll C(A); REP(i, m) if (A[i]) REP(j, m) if (A[j]) { int sum = (i + j) % m; if (C[sum] == 0) C[sum] = A[i] + A[j]; else C[sum] = min(C[sum], A[i] + A[j]); } return C; } int main() { SC2(n, k); SC(m); // FORE(i, 1, k) // zel[i].resize(m); REP(i, n) { int ki, mi, ci; SC2(ki, mi); SC(ci); if (mi == m) mi = 0; zel[ki].pb(mp(mi,ci)); /* if (zel[ki][mi] == 0) zel[ki][mi] = ci; else zel[ki][mi] = min(zel[ki][mi], (ll) ci);*/ } FORIT(it, zel[1], vii) { int mi = it->xx, ci = it->yy; if (t[1][mi] == 0) t[1][mi] = ci; else t[1][mi] = min(t[1][mi], (ll) ci); } /* FORE(i, 2, k) REP(j, m) { FORIT(it, zel[i], vii) { int mi = it->xx, ci = it->yy; int prev = j - mi; if (prev < 0) prev += m; if (t[i-1][prev] == 0) continue; if (t[i][j] == 0) t[i][j] = t[i-1][prev] + ci; else t[i][j] = min(t[i][j], t[i-1][prev] + ci); prd("set (%d,%d): %lld\n", i, j, t[i][j]); } } */ FORE(i, 2, k) REP(j, m) if (t[i-1][j]) { FORIT(it, zel[i], vii) { int mi = it->xx, ci = it->yy; int sum = j + mi; if (sum >= m) sum -= m; if (t[i][sum] == 0) t[i][sum] = t[i-1][j] + ci; else t[i][sum] = min(t[i][sum], t[i-1][j] + ci); //prd("set (%d,%d): %lld\n", i, j, t[i][j]); } } //printf("koniec fazy I\n"); REP(j, m) multi[1][j] = t[k][j]; int row = 1, pot = 1, changed = 1; while(changed) { //while(pot < 4 * m) { changed = 0; REP(i, m) multi[row+1][i] = multi[row][i]; REP(i, m) if (multi[row][i]) REP(j, m) if (multi[row][j]) { int sum = i + j; if (sum >= m) sum -= m; if (multi[row+1][sum] == 0 || multi[row][i] + multi[row][j] < multi[row+1][sum]) { changed = 1; multi[row+1][sum] = multi[row][i] + multi[row][j]; } //else // multi[row+1][sum] = min(multi[row+1][sum], multi[row][i] + multi[row][j]); } prd("ss pot %d\n", pot); dbg REP(j,m) prd("(%d): %lld\n", j, multi[row][j]); pot *= 2; row++; } /* vll ss; REP(j, m) ss.pb(t[k][j]); int pot = 1; while(pot < 4 * m) { ss = merge(ss); prd("ss pot %d\n", pot); dbg REP(j,m) prd("(%d): %lld\n", j, ss[j]); pot *= 2; } */ /* REP(i, m) { prd("%d: %lld\n", i, t[k][i]); multi[1][i] = t[k][i]; } FORE(i, 2, m) REP(j, m) { int prev = (i + m - j) % m; if (multi[1][j] == 0 || multi[i-1][prev] == 0) continue; if (multi[i][j] == 0) multi[i][j] = multi[1][j] + multi[i-1][prev]; else multi[i][j] = min(multi[i][i], multi[1][j] + multi[i-1][prev]); } */ PRL(0); FORE(i, 1, m-1) { if (multi[row][i] == 0) PRL(-1); else PRL(multi[row][i]); } 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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | #include<cstdio> #include<algorithm> #include<utility> #include<string> #include<vector> #include<map> #include<set> #include<queue> #include<stack> #include<cmath> #include<iostream> // macros #define FORE(c, a, b) for(int c=a; c<= int(b); ++c) #define FORD(c, a, b) for(int c=a; c>= int(b); --c) #define FORIT(it, cont, cont_t) for(cont_t::iterator it = cont.begin(); it != cont.end(); ++it) #define REP(i, n) for(int i = 0; i < (n); ++i) #define ALL(a) a.begin(), a.end() #define PR(n) printf("%d ", (int) (n)) #define PRL(n) printf("%lld\n", (ll) (n)) #define SC(n) scanf("%d", &n); #define SC2(n, m) scanf("%d%d", &n, &m); #define xx first #define yy second #define pb push_back #define mp make_pair #define itr iterator #define dbg if(0) #define prd dbg printf using namespace std; typedef long long ll; typedef long double ld; typedef unsigned int uint; typedef vector<int> vi; typedef vector<ll> vll; typedef set<int> si; typedef map<int, int> mi; typedef pair<int, int> pi; typedef vector<pi> vii; typedef vector<vi> vvi; const int NMAX = 7007; int n, m, k; ll t[NMAX][NMAX], multi[NMAX][NMAX]; vii zel[NMAX]; vll merge(vll A) { vll C(A); REP(i, m) if (A[i]) REP(j, m) if (A[j]) { int sum = (i + j) % m; if (C[sum] == 0) C[sum] = A[i] + A[j]; else C[sum] = min(C[sum], A[i] + A[j]); } return C; } int main() { SC2(n, k); SC(m); // FORE(i, 1, k) // zel[i].resize(m); REP(i, n) { int ki, mi, ci; SC2(ki, mi); SC(ci); if (mi == m) mi = 0; zel[ki].pb(mp(mi,ci)); /* if (zel[ki][mi] == 0) zel[ki][mi] = ci; else zel[ki][mi] = min(zel[ki][mi], (ll) ci);*/ } FORIT(it, zel[1], vii) { int mi = it->xx, ci = it->yy; if (t[1][mi] == 0) t[1][mi] = ci; else t[1][mi] = min(t[1][mi], (ll) ci); } /* FORE(i, 2, k) REP(j, m) { FORIT(it, zel[i], vii) { int mi = it->xx, ci = it->yy; int prev = j - mi; if (prev < 0) prev += m; if (t[i-1][prev] == 0) continue; if (t[i][j] == 0) t[i][j] = t[i-1][prev] + ci; else t[i][j] = min(t[i][j], t[i-1][prev] + ci); prd("set (%d,%d): %lld\n", i, j, t[i][j]); } } */ FORE(i, 2, k) REP(j, m) if (t[i-1][j]) { FORIT(it, zel[i], vii) { int mi = it->xx, ci = it->yy; int sum = j + mi; if (sum >= m) sum -= m; if (t[i][sum] == 0) t[i][sum] = t[i-1][j] + ci; else t[i][sum] = min(t[i][sum], t[i-1][j] + ci); //prd("set (%d,%d): %lld\n", i, j, t[i][j]); } } //printf("koniec fazy I\n"); REP(j, m) multi[1][j] = t[k][j]; int row = 1, pot = 1, changed = 1; while(changed) { //while(pot < 4 * m) { changed = 0; REP(i, m) multi[row+1][i] = multi[row][i]; REP(i, m) if (multi[row][i]) REP(j, m) if (multi[row][j]) { int sum = i + j; if (sum >= m) sum -= m; if (multi[row+1][sum] == 0 || multi[row][i] + multi[row][j] < multi[row+1][sum]) { changed = 1; multi[row+1][sum] = multi[row][i] + multi[row][j]; } //else // multi[row+1][sum] = min(multi[row+1][sum], multi[row][i] + multi[row][j]); } prd("ss pot %d\n", pot); dbg REP(j,m) prd("(%d): %lld\n", j, multi[row][j]); pot *= 2; row++; } /* vll ss; REP(j, m) ss.pb(t[k][j]); int pot = 1; while(pot < 4 * m) { ss = merge(ss); prd("ss pot %d\n", pot); dbg REP(j,m) prd("(%d): %lld\n", j, ss[j]); pot *= 2; } */ /* REP(i, m) { prd("%d: %lld\n", i, t[k][i]); multi[1][i] = t[k][i]; } FORE(i, 2, m) REP(j, m) { int prev = (i + m - j) % m; if (multi[1][j] == 0 || multi[i-1][prev] == 0) continue; if (multi[i][j] == 0) multi[i][j] = multi[1][j] + multi[i-1][prev]; else multi[i][j] = min(multi[i][i], multi[1][j] + multi[i-1][prev]); } */ PRL(0); FORE(i, 1, m-1) { if (multi[row][i] == 0) PRL(-1); else PRL(multi[row][i]); } return 0; } |