#include <cstdio> #include <map> using namespace std; int n, k, m, xk, xm, current_step, prev_t_idx, curr_t_idx, curr_queue_idx, next_queue_idx, curr_queue_items, next_queue_items; long long int xc; map<int, long long int> jelly[7001]; long long int final_table[7000], t[2][7000]; int step[7000], queue[2][7000]; bool final_table_updated; const long long int INF = 8888888888888888888ll; int main() { // read // printf("! read START\n"); scanf("%d %d %d", &n, &k, &m); for (int i = 0; i < n; i++) { scanf("%d %d %lld", &xk, &xm, &xc); map<int, long long int>::iterator it = jelly[xk].find(xm); if ((it == jelly[xk].end()) || (it->second > xc)) { jelly[xk][xm] = xc; } } // initialize // printf("! initialize START\n"); final_table[0] = 0; step[0] = 0; for (int i = 1; i < m; i++) { final_table[i] = INF; step[i] = -1; } curr_queue_idx = 0; next_queue_idx = 1; curr_queue_items = 1; next_queue_items = 0; queue[curr_queue_idx][0] = 0; current_step = 0; prev_t_idx = 0; curr_t_idx = 1; // main loop // printf("! main_loop START\n"); final_table_updated = true; while (final_table_updated) { final_table_updated = false; // (a) iterater over all colors for (int curr_k = 1; curr_k <= k; curr_k++) { current_step++; // (b) within color, iterater over all jellies of that color for (map<int, long long int>::iterator it_k = jelly[curr_k].begin(); it_k != jelly[curr_k].end(); it_k++) { // (c) iterate through queue which contains points where jelly in previous step was put for (int curr_q = 0; curr_q < curr_queue_items; curr_q++) { int last_index = queue[curr_queue_idx][curr_q]; int next_index = (last_index + it_k->first) % m; long long int new_value = t[prev_t_idx][last_index] + it_k->second; if (step[next_index] < current_step) { t[curr_t_idx][next_index] = new_value; step[next_index] = current_step; // add to queue queue[next_queue_idx][next_queue_items] = next_index; next_queue_items++; } else if (t[curr_t_idx][next_index] > new_value) { t[curr_t_idx][next_index] = new_value; // do not add to the queue, as it was already added } } } // prepare for the next step if (curr_queue_idx == 0) { curr_queue_idx = 1; next_queue_idx = 0; } else { curr_queue_idx = 0; next_queue_idx = 1; } curr_queue_items = next_queue_items; next_queue_items = 0; if (prev_t_idx == 0) { prev_t_idx = 1; curr_t_idx = 0; } else { prev_t_idx = 0; curr_t_idx = 1; } } // update potentially our final table for (int curr_q = 0; curr_q < curr_queue_items; curr_q++) { int this_index = queue[curr_queue_idx][curr_q]; if (t[prev_t_idx][this_index] < final_table[this_index]) { final_table[this_index] = t[prev_t_idx][this_index]; final_table_updated = true; // add to queue queue[next_queue_idx][next_queue_items] = this_index; next_queue_items++; } } // prepare for starting once again (switch queues) if (curr_queue_idx == 0) { curr_queue_idx = 1; next_queue_idx = 0; } else { curr_queue_idx = 0; next_queue_idx = 1; } curr_queue_items = next_queue_items; next_queue_items = 0; } // print results // printf("! print_results START\n"); for (int i = 0; i < m; i++) { if (final_table[i] == INF) { printf("-1\n"); } else { printf("%lld\n", final_table[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 | #include <cstdio> #include <map> using namespace std; int n, k, m, xk, xm, current_step, prev_t_idx, curr_t_idx, curr_queue_idx, next_queue_idx, curr_queue_items, next_queue_items; long long int xc; map<int, long long int> jelly[7001]; long long int final_table[7000], t[2][7000]; int step[7000], queue[2][7000]; bool final_table_updated; const long long int INF = 8888888888888888888ll; int main() { // read // printf("! read START\n"); scanf("%d %d %d", &n, &k, &m); for (int i = 0; i < n; i++) { scanf("%d %d %lld", &xk, &xm, &xc); map<int, long long int>::iterator it = jelly[xk].find(xm); if ((it == jelly[xk].end()) || (it->second > xc)) { jelly[xk][xm] = xc; } } // initialize // printf("! initialize START\n"); final_table[0] = 0; step[0] = 0; for (int i = 1; i < m; i++) { final_table[i] = INF; step[i] = -1; } curr_queue_idx = 0; next_queue_idx = 1; curr_queue_items = 1; next_queue_items = 0; queue[curr_queue_idx][0] = 0; current_step = 0; prev_t_idx = 0; curr_t_idx = 1; // main loop // printf("! main_loop START\n"); final_table_updated = true; while (final_table_updated) { final_table_updated = false; // (a) iterater over all colors for (int curr_k = 1; curr_k <= k; curr_k++) { current_step++; // (b) within color, iterater over all jellies of that color for (map<int, long long int>::iterator it_k = jelly[curr_k].begin(); it_k != jelly[curr_k].end(); it_k++) { // (c) iterate through queue which contains points where jelly in previous step was put for (int curr_q = 0; curr_q < curr_queue_items; curr_q++) { int last_index = queue[curr_queue_idx][curr_q]; int next_index = (last_index + it_k->first) % m; long long int new_value = t[prev_t_idx][last_index] + it_k->second; if (step[next_index] < current_step) { t[curr_t_idx][next_index] = new_value; step[next_index] = current_step; // add to queue queue[next_queue_idx][next_queue_items] = next_index; next_queue_items++; } else if (t[curr_t_idx][next_index] > new_value) { t[curr_t_idx][next_index] = new_value; // do not add to the queue, as it was already added } } } // prepare for the next step if (curr_queue_idx == 0) { curr_queue_idx = 1; next_queue_idx = 0; } else { curr_queue_idx = 0; next_queue_idx = 1; } curr_queue_items = next_queue_items; next_queue_items = 0; if (prev_t_idx == 0) { prev_t_idx = 1; curr_t_idx = 0; } else { prev_t_idx = 0; curr_t_idx = 1; } } // update potentially our final table for (int curr_q = 0; curr_q < curr_queue_items; curr_q++) { int this_index = queue[curr_queue_idx][curr_q]; if (t[prev_t_idx][this_index] < final_table[this_index]) { final_table[this_index] = t[prev_t_idx][this_index]; final_table_updated = true; // add to queue queue[next_queue_idx][next_queue_items] = this_index; next_queue_items++; } } // prepare for starting once again (switch queues) if (curr_queue_idx == 0) { curr_queue_idx = 1; next_queue_idx = 0; } else { curr_queue_idx = 0; next_queue_idx = 1; } curr_queue_items = next_queue_items; next_queue_items = 0; } // print results // printf("! print_results START\n"); for (int i = 0; i < m; i++) { if (final_table[i] == INF) { printf("-1\n"); } else { printf("%lld\n", final_table[i]); } } return 0; } |