// Autor: Nicolas Wochnik
#include <climits>
#include <iostream>
#include <unordered_map>
#include <vector>
long long modulus;
std::vector<std::unordered_map<long long, long long>> moves;
std::vector<long long> answer, next_answer;
void load_input() {
size_t n, k;
std::cin >> n >> k >> modulus;
moves.resize(k);
for (size_t i = 0; i < n; ++i) {
long long color, mass, price;
std::cin >> color >> mass >> price;
color--;
if (moves[color].count(mass)) {
moves[color][mass] = std::min(moves[color][mass], price);
continue;
}
moves[color][mass] = price;
}
}
void add_moves(size_t i) {
for (auto &item : next_answer) {
item = -1;
}
for (size_t j = 0; j < modulus; ++j) {
for (const auto &[jump, price] : moves[i]) {
size_t next_j = (j + jump) % modulus;
if (answer[j] == -1) {
continue;
}
if (next_answer[next_j] == -1) {
next_answer[next_j] = answer[j] + price;
continue;
}
next_answer[next_j] = std::min(next_answer[next_j], answer[j] + price);
}
}
answer = next_answer;
}
void djikstra() {
for (auto &item : next_answer) {
item = LLONG_MAX;
}
next_answer[0] = 0;
std::vector<bool> visited(modulus, false);
for (size_t iter = 0; iter < modulus; ++iter) {
long long i = -1;
for (long long j = 0; j < modulus; j++) {
if (visited[j] || next_answer[j] == -1 ||
(i != -1 && next_answer[j] >= next_answer[i])) {
continue;
}
i = j;
}
visited[i] = true;
for (size_t ni = 0; ni < modulus; ++ni) {
auto n_index = (i + ni) % modulus;
auto price = answer[ni];
if (price == -1 || visited[n_index]) {
continue;
}
next_answer[n_index] =
std::min(next_answer[n_index], next_answer[i] + price);
}
}
for (auto &item : next_answer) {
if (item == LLONG_MAX) {
item = -1;
}
}
answer = next_answer;
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
load_input();
answer.resize(modulus);
next_answer.resize(modulus);
for (auto &item : answer) {
item = -1;
}
answer[0] = 0;
for (size_t i = 0; i < moves.size(); ++i) {
add_moves(i);
}
djikstra();
for (auto x : answer) {
std::cout << x << "\n";
}
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 | // Autor: Nicolas Wochnik #include <climits> #include <iostream> #include <unordered_map> #include <vector> long long modulus; std::vector<std::unordered_map<long long, long long>> moves; std::vector<long long> answer, next_answer; void load_input() { size_t n, k; std::cin >> n >> k >> modulus; moves.resize(k); for (size_t i = 0; i < n; ++i) { long long color, mass, price; std::cin >> color >> mass >> price; color--; if (moves[color].count(mass)) { moves[color][mass] = std::min(moves[color][mass], price); continue; } moves[color][mass] = price; } } void add_moves(size_t i) { for (auto &item : next_answer) { item = -1; } for (size_t j = 0; j < modulus; ++j) { for (const auto &[jump, price] : moves[i]) { size_t next_j = (j + jump) % modulus; if (answer[j] == -1) { continue; } if (next_answer[next_j] == -1) { next_answer[next_j] = answer[j] + price; continue; } next_answer[next_j] = std::min(next_answer[next_j], answer[j] + price); } } answer = next_answer; } void djikstra() { for (auto &item : next_answer) { item = LLONG_MAX; } next_answer[0] = 0; std::vector<bool> visited(modulus, false); for (size_t iter = 0; iter < modulus; ++iter) { long long i = -1; for (long long j = 0; j < modulus; j++) { if (visited[j] || next_answer[j] == -1 || (i != -1 && next_answer[j] >= next_answer[i])) { continue; } i = j; } visited[i] = true; for (size_t ni = 0; ni < modulus; ++ni) { auto n_index = (i + ni) % modulus; auto price = answer[ni]; if (price == -1 || visited[n_index]) { continue; } next_answer[n_index] = std::min(next_answer[n_index], next_answer[i] + price); } } for (auto &item : next_answer) { if (item == LLONG_MAX) { item = -1; } } answer = next_answer; } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); load_input(); answer.resize(modulus); next_answer.resize(modulus); for (auto &item : answer) { item = -1; } answer[0] = 0; for (size_t i = 0; i < moves.size(); ++i) { add_moves(i); } djikstra(); for (auto x : answer) { std::cout << x << "\n"; } return 0; } |
English