#include <iostream>
#include <list>
#include <unordered_map>
#include <map>
#include <vector>
#include <string>
#include <array>
constexpr int main_city = 1;
struct city {
int from;
int to;
int cost;
};
struct city_list {
std::list<city> list;
int sum;
};
const std::unordered_map<char, std::array<bool, 4>> shortAscii {
{'0', {0, 0, 0, 0}},
{'1', {0, 0, 0, 1}},
{'2', {0, 0, 1, 0}},
{'3', {0, 0, 1, 1}},
{'4', {0, 1, 0, 0}},
{'5', {0, 1, 0, 1}},
{'6', {0, 1, 1, 0}},
{'7', {0, 1, 1, 1}},
{'8', {1, 0, 0, 0}},
{'9', {1, 0, 0, 1}},
{2, {1, 0, 1, 0}}, //STX 10
{3, {1, 0, 1, 1}}, //ETX 11
{' ', {1, 1, 0, 0}}, //SPACE 12
{4, {1, 1, 0, 1}}, //EOT 13
};
std::string getKey(std::list<city> cs) {
city f = cs.front();
city l = cs.back();
return std::to_string(f.from) + "-" + std::to_string(f.to) + "-" + std::to_string(f.cost) + "|" + std::to_string(l.from) + "-" + std::to_string(l.to) + "-" + std::to_string(l.cost);
}
void addPath(std::unordered_map<std::string, std::list<city>> &paths, city c) {
int from = c.from, to = c.to;
if (c.to < c.from) {
from = c.to;
to = c.from;
}
bool found = false;
for (auto [key, path] : paths) {
auto last = &path.back();
auto first = &path.front();
if (last->from == from && last->to == to) { //If exists (but should not), update cost when lower
if (last->cost > c.cost) {
last->cost = c.cost;
}
found = true;
}
else if (last->to == from) { //If found next, add to existing and duplicate
paths[getKey(path)] = path;
path.push_back(city{from, to, c.cost});
paths[getKey(path)] = path;
found = true;
}
else if (first->from == to) { //If found previous, prepend to existing
paths.erase(getKey(path));
path.push_front(city{from, to, c.cost});
paths[getKey(path)] = path;
found = true;
}
}
if (!found) {
std::list<city> list = {city{from, to, c.cost}};
paths[getKey(list)] = list;
}
}
void calculateMinCost(std::map<std::string, int> &cost_map, std::list<city> path) {
int sum = 0;
for (auto c : path) {
// std::cout << "(" << city.from << " " << city.to << " " << city.cost << ") ";
sum += c.cost;
}
// std::cout << city_l.front().from << " " << city_l.back().to << " " << sum << std::endl;
std::string new_key = std::to_string(path.front().from) + " " + std::to_string(path.back().to);
if (!cost_map.contains(new_key)) {
cost_map[new_key] = sum;
}
else if (cost_map[new_key] > sum) {
cost_map[new_key] = sum;
}
}
/*
std::map<std::string, int> lowestCost() {
auto *cities = new std::vector<city>();
cities->push_back(city{1, 2, 6});
cities->push_back(city{3, 2, 4});
cities->push_back(city{3, 1, 10});
cities->push_back(city{2, 3, 3});
cities->push_back(city{4, 2, 1});
cities->push_back(city{4, 3, 3});
cities->push_back(city{4, 1, 7});
std::unordered_map<std::string, std::list<city>> paths_hashmap;
std::map<std::string, int> cost_map;
for (const city c : *cities) {
addPath(paths_hashmap, c);
}
delete cities;
for (auto path_pair: paths_hashmap) {
calculateMinCost(cost_map, path_pair.second);
}
return cost_map;
}
*/
std::vector<bool> encode(std::vector<std::string> payload) {
auto msg = std::vector<bool>();
//City
for (auto cities : payload) {
//STX
for (auto flag : shortAscii.at(2)) {
msg.push_back(flag);
}
//data
for (auto num : cities) {
for (auto flag : shortAscii.at(num)) {
msg.push_back(flag);
}
}
//ETX
for (auto flag : shortAscii.at(3)) {
msg.push_back(flag);
}
}
//EOT
for (auto flag : shortAscii.at(4)) {
msg.push_back(flag);
}
return msg;
}
city strtocity(std::string c) {
int pos = c.find(' ');
std::string p1 = c.substr(0, pos);
std::string rest = c.substr(pos + 1);
int pos2 = rest.find(' ');
int from = atoi(p1.c_str());
int to = atoi(rest.substr(0, pos2).c_str());
int cost = atoi(rest.substr(pos2 + 1).c_str());
return city{from, to, cost };
}
/*
std::vector<city> decode(std::vector<bool> payload) {
std::vector<city> result;
std::string s = "";
for (int i = 0; i < payload.size()/4; i++) {
int num = payload.at(i*4+0)*8 + payload.at(i*4+1)*4 + payload.at(i*4+2)*2 + payload.at(i*4+3);
if (num == 10) {
s.clear();
}
else if (num < 10) { //number or space
s += std::to_string(num);
}
else if (num == 12) { //number or space
s += ' ';
}
else if (num == 11) {
result.push_back(strtocity(s));
}
else if (num == 13) {
break;
}
}
return result;
}
*/
std::vector<city> decodeFromCin() {
std::vector<city> result;
std::string s = "";
while (true) {
bool val;
bool payload[4] = {};
for (int i = 0; i < 4; i++) {
std::cout << "?" << "\n";
std::cout.flush();
std::cin >> val;
payload[i] = val;
}
int num = payload[0] * 8 + payload[1] * 4 + payload[2] * 2 + payload[3];
if (num == 10) {
s.clear();
}
else if (num < 10) { //number or space
s += std::to_string(num);
}
else if (num == 12) { //number or space
s += ' ';
}
else if (num == 11) {
result.push_back(strtocity(s));
}
else if (num == 13) {
break;
}
}
return result;
}
void printResult(std::map<std::string, int> cost_map, const std::vector<city>& fromBajtek, int cities_count) {
for (auto city : fromBajtek) {
std::string new_key = std::to_string(city.from) + " " + std::to_string(city.to);
if (cost_map.contains(new_key)) {
if (cost_map.at(new_key) > city.cost) {
cost_map[new_key] = city.cost;
}
}
else {
cost_map[new_key] = city.cost;
}
}
std::cout << "!";
for (int i = 1; i <= cities_count; i++) {
std::cout << " ";
std::string new_key = "1 " + std::to_string(i);
if (!cost_map.contains(new_key)) {
std::cout << "0";
}
else {
std::cout << cost_map.at(new_key);
}
}
std::cout << "\n";
std::cout.flush();
}
int main() {
std::unordered_map<std::string, std::list<city>> paths_hashmap;
std::map<std::string, int> cost_map;
std::string name;
std::cin >> name;
int cities_count = 0, connections = 0;
std::cin >> cities_count >> connections;
for (int i = 0; i < connections; i++) {
int from, to, cost;
std::cin >> from >> to >> cost;
addPath(paths_hashmap, city{from, to, cost});
}
for (auto path_pair: paths_hashmap) {
calculateMinCost(cost_map, path_pair.second);
}
if (name == "Bajtek") {
std::vector<std::string> payload;
payload.reserve(cost_map.size());
for (auto [key, cost]: cost_map) {
payload.push_back(key + " " + std::to_string(cost));
}
std::vector<bool> toSend = encode(payload);
for (auto flag: toSend) {
std::cout << "+ " << flag << "\n";
std::cout.flush();
}
}
else {
auto result = decodeFromCin();
printResult(cost_map, result, cities_count);
}
return 0;
}
// int main() {
// std::vector<std::string> payload;
// for (auto [key, cost]: lowestCost()) {
// payload.push_back(key + " " + std::to_string(cost));
// }
// std::vector<bool> received = encode(payload);
// std::vector<city> cities = decode(received);
// std::cout << "ok" << std::endl;
// return 0;
// }
/*
int main() {
std::unordered_map<std::string, std::list<city>> paths_hashmap;
std::map<std::string, int> cost_map;
std::string name;
std::cin >> name;
int cities_count = 0, connections = 0;
std::cin >> cities_count >> connections;
for (int i = 0; i < connections; i++) {
int from, to, cost;
std::cin >> from >> to >> cost;
addPath(paths_hashmap, city{from, to, cost});
}
for (auto path_pair: paths_hashmap) {
calculateMinCost(cost_map, path_pair.second);
}
if (name == "Bajtek") {
std::vector<std::string> payload;
payload.reserve(cost_map.size());
for (auto [key, cost]: cost_map) {
payload.push_back(key + " " + std::to_string(cost));
}
std::vector<bool> toSend = encode(payload);
for (auto flag: toSend) {
std::cout << "+ " << flag << "\n";
std::cout.flush();
}
}
else {
auto result = decodeFromCin();
printResult(cost_map, result, cities_count);
}
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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 | #include <iostream> #include <list> #include <unordered_map> #include <map> #include <vector> #include <string> #include <array> constexpr int main_city = 1; struct city { int from; int to; int cost; }; struct city_list { std::list<city> list; int sum; }; const std::unordered_map<char, std::array<bool, 4>> shortAscii { {'0', {0, 0, 0, 0}}, {'1', {0, 0, 0, 1}}, {'2', {0, 0, 1, 0}}, {'3', {0, 0, 1, 1}}, {'4', {0, 1, 0, 0}}, {'5', {0, 1, 0, 1}}, {'6', {0, 1, 1, 0}}, {'7', {0, 1, 1, 1}}, {'8', {1, 0, 0, 0}}, {'9', {1, 0, 0, 1}}, {2, {1, 0, 1, 0}}, //STX 10 {3, {1, 0, 1, 1}}, //ETX 11 {' ', {1, 1, 0, 0}}, //SPACE 12 {4, {1, 1, 0, 1}}, //EOT 13 }; std::string getKey(std::list<city> cs) { city f = cs.front(); city l = cs.back(); return std::to_string(f.from) + "-" + std::to_string(f.to) + "-" + std::to_string(f.cost) + "|" + std::to_string(l.from) + "-" + std::to_string(l.to) + "-" + std::to_string(l.cost); } void addPath(std::unordered_map<std::string, std::list<city>> &paths, city c) { int from = c.from, to = c.to; if (c.to < c.from) { from = c.to; to = c.from; } bool found = false; for (auto [key, path] : paths) { auto last = &path.back(); auto first = &path.front(); if (last->from == from && last->to == to) { //If exists (but should not), update cost when lower if (last->cost > c.cost) { last->cost = c.cost; } found = true; } else if (last->to == from) { //If found next, add to existing and duplicate paths[getKey(path)] = path; path.push_back(city{from, to, c.cost}); paths[getKey(path)] = path; found = true; } else if (first->from == to) { //If found previous, prepend to existing paths.erase(getKey(path)); path.push_front(city{from, to, c.cost}); paths[getKey(path)] = path; found = true; } } if (!found) { std::list<city> list = {city{from, to, c.cost}}; paths[getKey(list)] = list; } } void calculateMinCost(std::map<std::string, int> &cost_map, std::list<city> path) { int sum = 0; for (auto c : path) { // std::cout << "(" << city.from << " " << city.to << " " << city.cost << ") "; sum += c.cost; } // std::cout << city_l.front().from << " " << city_l.back().to << " " << sum << std::endl; std::string new_key = std::to_string(path.front().from) + " " + std::to_string(path.back().to); if (!cost_map.contains(new_key)) { cost_map[new_key] = sum; } else if (cost_map[new_key] > sum) { cost_map[new_key] = sum; } } /* std::map<std::string, int> lowestCost() { auto *cities = new std::vector<city>(); cities->push_back(city{1, 2, 6}); cities->push_back(city{3, 2, 4}); cities->push_back(city{3, 1, 10}); cities->push_back(city{2, 3, 3}); cities->push_back(city{4, 2, 1}); cities->push_back(city{4, 3, 3}); cities->push_back(city{4, 1, 7}); std::unordered_map<std::string, std::list<city>> paths_hashmap; std::map<std::string, int> cost_map; for (const city c : *cities) { addPath(paths_hashmap, c); } delete cities; for (auto path_pair: paths_hashmap) { calculateMinCost(cost_map, path_pair.second); } return cost_map; } */ std::vector<bool> encode(std::vector<std::string> payload) { auto msg = std::vector<bool>(); //City for (auto cities : payload) { //STX for (auto flag : shortAscii.at(2)) { msg.push_back(flag); } //data for (auto num : cities) { for (auto flag : shortAscii.at(num)) { msg.push_back(flag); } } //ETX for (auto flag : shortAscii.at(3)) { msg.push_back(flag); } } //EOT for (auto flag : shortAscii.at(4)) { msg.push_back(flag); } return msg; } city strtocity(std::string c) { int pos = c.find(' '); std::string p1 = c.substr(0, pos); std::string rest = c.substr(pos + 1); int pos2 = rest.find(' '); int from = atoi(p1.c_str()); int to = atoi(rest.substr(0, pos2).c_str()); int cost = atoi(rest.substr(pos2 + 1).c_str()); return city{from, to, cost }; } /* std::vector<city> decode(std::vector<bool> payload) { std::vector<city> result; std::string s = ""; for (int i = 0; i < payload.size()/4; i++) { int num = payload.at(i*4+0)*8 + payload.at(i*4+1)*4 + payload.at(i*4+2)*2 + payload.at(i*4+3); if (num == 10) { s.clear(); } else if (num < 10) { //number or space s += std::to_string(num); } else if (num == 12) { //number or space s += ' '; } else if (num == 11) { result.push_back(strtocity(s)); } else if (num == 13) { break; } } return result; } */ std::vector<city> decodeFromCin() { std::vector<city> result; std::string s = ""; while (true) { bool val; bool payload[4] = {}; for (int i = 0; i < 4; i++) { std::cout << "?" << "\n"; std::cout.flush(); std::cin >> val; payload[i] = val; } int num = payload[0] * 8 + payload[1] * 4 + payload[2] * 2 + payload[3]; if (num == 10) { s.clear(); } else if (num < 10) { //number or space s += std::to_string(num); } else if (num == 12) { //number or space s += ' '; } else if (num == 11) { result.push_back(strtocity(s)); } else if (num == 13) { break; } } return result; } void printResult(std::map<std::string, int> cost_map, const std::vector<city>& fromBajtek, int cities_count) { for (auto city : fromBajtek) { std::string new_key = std::to_string(city.from) + " " + std::to_string(city.to); if (cost_map.contains(new_key)) { if (cost_map.at(new_key) > city.cost) { cost_map[new_key] = city.cost; } } else { cost_map[new_key] = city.cost; } } std::cout << "!"; for (int i = 1; i <= cities_count; i++) { std::cout << " "; std::string new_key = "1 " + std::to_string(i); if (!cost_map.contains(new_key)) { std::cout << "0"; } else { std::cout << cost_map.at(new_key); } } std::cout << "\n"; std::cout.flush(); } int main() { std::unordered_map<std::string, std::list<city>> paths_hashmap; std::map<std::string, int> cost_map; std::string name; std::cin >> name; int cities_count = 0, connections = 0; std::cin >> cities_count >> connections; for (int i = 0; i < connections; i++) { int from, to, cost; std::cin >> from >> to >> cost; addPath(paths_hashmap, city{from, to, cost}); } for (auto path_pair: paths_hashmap) { calculateMinCost(cost_map, path_pair.second); } if (name == "Bajtek") { std::vector<std::string> payload; payload.reserve(cost_map.size()); for (auto [key, cost]: cost_map) { payload.push_back(key + " " + std::to_string(cost)); } std::vector<bool> toSend = encode(payload); for (auto flag: toSend) { std::cout << "+ " << flag << "\n"; std::cout.flush(); } } else { auto result = decodeFromCin(); printResult(cost_map, result, cities_count); } return 0; } // int main() { // std::vector<std::string> payload; // for (auto [key, cost]: lowestCost()) { // payload.push_back(key + " " + std::to_string(cost)); // } // std::vector<bool> received = encode(payload); // std::vector<city> cities = decode(received); // std::cout << "ok" << std::endl; // return 0; // } /* int main() { std::unordered_map<std::string, std::list<city>> paths_hashmap; std::map<std::string, int> cost_map; std::string name; std::cin >> name; int cities_count = 0, connections = 0; std::cin >> cities_count >> connections; for (int i = 0; i < connections; i++) { int from, to, cost; std::cin >> from >> to >> cost; addPath(paths_hashmap, city{from, to, cost}); } for (auto path_pair: paths_hashmap) { calculateMinCost(cost_map, path_pair.second); } if (name == "Bajtek") { std::vector<std::string> payload; payload.reserve(cost_map.size()); for (auto [key, cost]: cost_map) { payload.push_back(key + " " + std::to_string(cost)); } std::vector<bool> toSend = encode(payload); for (auto flag: toSend) { std::cout << "+ " << flag << "\n"; std::cout.flush(); } } else { auto result = decodeFromCin(); printResult(cost_map, result, cities_count); } return 0; } */ |
English