// Protocol description. // // Stage 1. Each node finds its out edges. // Stage 2. Out edge information transferred to node 0. // Stage 3. Node 0 does planning who sends data which way. // Stage 4. Node 0 sends out orders. // Stage 5. Root gets notified. // Stage 6. Data gets sent. // Stage 7. Final node sends out halt signal to everyone else. // Stage 8. Final node prints out answer, everyone else halts. // // Total cost: 500+300+200+100+100=1200. #include "message.h" #include "skup.h" #include <algorithm> #include <iostream> #include <vector> using namespace std; bool is_sink = false; int my_dest = -1, sink = -1; vector<bool> in_edge, out_edge; vector<int> prices, dest; vector<vector<bool> > edge, reached; // Stage 1. // - Node 0 pokes smaller half of the remaining nodes. // - Each node either pokes back directly or via a twin. If poked directly, node 0 pokes the twin and waits for a poke-back. // - Repeat 3 times to cover all nodes. // - Repeat for all nodes. // Cost: 200 for chosen, 3 for other. Total 500. void Stage1() { in_edge.resize(NumberOfNodes()); out_edge.resize(NumberOfNodes()); for (int i = 0; i < NumberOfNodes(); ++i) { vector<int> other; for (int j = 1; j < NumberOfNodes(); ++j) other.push_back((i + j) % NumberOfNodes()); int unpaired = other.back(); other.resize(other.size() - 1); vector<int> group(NumberOfNodes()); vector<int> twin(NumberOfNodes()); for (int j = 0; j < other.size(); j += 2) { twin[other[j ]] = other[j + 1]; twin[other[j + 1]] = other[j ]; group[other[j ]] = 1; group[other[j + 1]] = 2; } if (MyNodeId() == i) { int wait; // cerr << "Source start" << endl; for (int j = 0; j < other.size(); j += 2) { PutChar(other[j], 1); Send(other[j]); } vector<int> pokes; for (int j = 0; j < other.size(); j += 2) { int source = Receive(-1); GetChar(source); if (group[source] == 1) { out_edge[source] = true; // cerr << "(group 1)->" << source << endl; pokes.push_back(twin[source]); } else { // cerr << "Negative message from " << source << endl; } } for (int i = 0; i < pokes.size(); ++i) { PutChar(pokes[i], 0); Send(pokes[i]); } // cerr << "Waiting for " << wait << " dummies" << endl; while (pokes.size() > 0) { int source = Receive(-1); // cerr << "Dummy message from " << source << endl; GetChar(source); pokes.resize(pokes.size() - 1); } // cerr << "Source finsihed handling group 1" << endl; for (int j = 1; j < other.size(); j += 2) { PutChar(other[j], 1); Send(other[j]); } for (int j = 0; j < other.size(); j += 2) { int source = Receive(-1); GetChar(source); if (group[source] == 2) { out_edge[source] = true; // cerr << "(group 2)->" << source << endl; pokes.push_back(twin[source]); } else { // cerr << "Negative message from " << source << endl; } } for (int i = 0; i < pokes.size(); ++i) { PutChar(pokes[i], 0); Send(pokes[i]); } // cerr << "Waiting for " << wait << " dummies" << endl; while (pokes.size() > 0) { int source = Receive(-1); // cerr << "Dummy message from " << source << endl; GetChar(source); pokes.resize(pokes.size() - 1); } // cerr << "Source finsihed handling group 2" << endl; PutChar(unpaired, 1); Send(unpaired); int source = Receive(-1); GetChar(source); if (source == unpaired) { out_edge[source] = true; // cerr << "(unpaired)->" << source << endl; PutChar(other[0], 0); Send(other[0]); Receive(other[0]); GetChar(other[0]); } // cerr << "Source done" << endl; } else if (MyNodeId() == unpaired) { // cerr << "Unpaired start" << endl; Receive(i); if (GetChar(i) == 1) { // cerr << "<-" << i << endl; in_edge[i] = true; PutChar(i, 0); Send(i); } else { // cerr << "Bumping to other[0]" << endl; PutChar(other[0], 0); Send(other[0]); } // cerr << "Unpaired done" << endl; } else if (group[MyNodeId()] == 1) { // cerr << "Group 1 start" << endl; Receive(i); if (GetChar(i) == 1) { in_edge[i] = true; // cerr << "<-" << i << endl; PutChar(i, 0); Send(i); } else { // cerr << "Bumping to twin" << endl; PutChar(twin[MyNodeId()], 0); Send(twin[MyNodeId()]); } int source = Receive(-1); // cerr << "Bump from " << source << endl; GetChar(source); PutChar(i, 0); Send(i); // cerr << "Group 1 done" << endl; if (MyNodeId() == other[0]) { // cerr << "Other[0] start" << endl; int source = Receive(-1); // cerr << "Bump from " << source << endl; GetChar(source); PutChar(i, 0); Send(i); // cerr << "Other[0] done" << endl; } } else { // cerr << "Group 2 start" << endl; int source = Receive(-1); // cerr << "Bump from " << source << endl; GetChar(source); PutChar(i, 0); Send(i); Receive(i); if (GetChar(i) == 1) { in_edge[i] = true; // cerr << "<-" << i << endl; PutChar(i, 0); Send(i); } else { // cerr << "Bumping to twin" << endl; PutChar(twin[MyNodeId()], 0); Send(twin[MyNodeId()]); } // cerr << "Group 2 done" << endl; } } } // Stage 2. // - Node 0 pokes node 1, gets poked back directly or via another node, indicating the number of out edges. // - Node 0 pokes node 1, gets poked back by out edges. // - Repeat for all nodes. // - Node 0 pokes everyone once again to mark the end. // Cost: 3 for 0, 100 for chosen, 2 for other. Total 300. void Stage2() { if (MyNodeId() == 0) { edge.resize(NumberOfNodes(), vector<bool>(NumberOfNodes())); for (int i = 0; i < NumberOfNodes(); ++i) if (out_edge[i]) edge[MyNodeId()][i] = true; for (int i = 0; i < NumberOfNodes(); ++i) if (in_edge[i]) edge[i][MyNodeId()] = true; for (int i = 1; i < NumberOfNodes(); ++i) { PutChar(i, 0); Send(i); // cerr << "Sent out request to " << i << endl; int out_count = Receive(-1); GetChar(out_count--); // cerr << "Received response with out_count = " << out_count << endl; PutChar(i, 0); Send(i); while (out_count--) { // cerr << "Requesting an edge " << endl; int source = Receive(-1); // cerr << "Received edge from " << source << endl; GetChar(source); edge[i][source] = true; } } for (int i = 1; i < NumberOfNodes(); ++i) { // cerr << "Pushing " << i << " past final stage" << endl; PutChar(i, 0); Send(i); } // cerr << "Source node done" << endl; } else { int stage = 0; while (stage < 3) { // cerr << "Stage " << stage << endl; int source; source = Receive(-1); GetChar(source); if (source != 0) { // cerr << "Forwaring information from " << source << endl; PutChar(0, 0); Send(0); continue; } if (stage == 0) { int out_count = 1; for (int j = 1; j < NumberOfNodes(); ++j) { if (out_edge[j]) { // cerr << "Counting edge " << MyNodeId() << "->" << j << endl; ++out_count; } } // cerr << "Sending back out_count = " << out_count << endl; if (out_count == MyNodeId()) { PutChar(0, 0); Send(0); } else { PutChar(out_count, 0); Send(out_count); } } else if (stage == 1) { for (int j = 1; j < NumberOfNodes(); ++j) if (out_edge[j]) { // cerr << "Sending back edge to " << j << endl; PutChar(j, 0); Send(j); } } ++stage; } } } void Stage3Dfs(const int a, const int b) { if (reached[a][b]) return; reached[a][b] = true; for (int i = 0; i < NumberOfNodes(); ++i) if (edge[b][i]) Stage3Dfs(a, i); } void Stage3ReverseDfs(const int a, const int b) { if (dest[a] >= 0) return; dest[a] = b; for (int i = 0; i < NumberOfNodes(); ++i) if (edge[i][a]) Stage3ReverseDfs(i, a); } // Stage 3. // - All in node 0's mind. void Stage3() { if (MyNodeId() == 0) { // for (int i = 0; i < NumberOfNodes(); ++i) for (int j = 0; j < NumberOfNodes(); ++j) if (edge[i][j]) cerr << i << "->" << j << endl; reached.resize(NumberOfNodes(), vector<bool>(NumberOfNodes())); for (int i = 0; i < NumberOfNodes(); ++i) Stage3Dfs(i, i); for (int i = 0; i < NumberOfNodes(); ++i) { bool all_in = true; bool all_out = true; for (int j = 0; j < NumberOfNodes(); ++j) if (!reached[j][i]) all_in = false; for (int j = 0; j < NumberOfNodes(); ++j) if (i != j && !edge[i][j]) all_out = false; if (all_in && !all_out) sink = i; } if (sink == -1) sink = 0; // cerr << "Sink " << sink << endl; dest.resize(NumberOfNodes(), -1); if (sink == 0) Stage3ReverseDfs(0, 0); else { int invalid_edge = -1; for (int i = 0; i < NumberOfNodes(); ++i) if (i != sink && !edge[sink][i]) invalid_edge = i; // cerr << invalid_edge << endl; Stage3ReverseDfs(sink, invalid_edge); } // for (int i = 0; i < NumberOfNodes(); ++i) if (i != sink) cerr << i << "->" << dest[i] << endl; my_dest = dest[0]; is_sink = sink == 0; } } // Stage 4. // - Node 1's destination pokes node 1, possibly poked by node 0 first. // - Node 1 notifies everyone that we're moving on to Node 2. // - Repeat for all nodes. // Cost: 1 for 0, 100 for chosen, 1 for other. Total 200. void Stage4() { if (MyNodeId() == 0) { for (int i = 1; i < NumberOfNodes(); ++i) { // cerr << "Notifying " << i << " of dest " << dest[i] << endl; if (dest[i] == 0) { PutChar(i, 0); Send(i); } else { PutChar(dest[i], 0); Send(dest[i]); } // cerr << "Waiting for ack from " << i << endl; Receive(i); GetChar(i); // cerr << "Ok to move on to next one" << endl; } } else { int current = 1; while (current < NumberOfNodes()) { int source = Receive(-1); GetChar(source); if (MyNodeId() == current) { // cerr << "Received my destination " << source << endl; my_dest = source; for (int i = 1; i < NumberOfNodes(); ++i) if (i != MyNodeId()) { // cerr << "Broadcasting update of current to " << i << endl; PutChar(i, 0); Send(i); } for (int i = 1; i < NumberOfNodes(); ++i) if (i != MyNodeId()) { // cerr << "Waiting for ack at " << i << endl; source = Receive(-1); GetChar(source); // cerr << "Got ack at " << i << endl; } // cerr << "Letting know source it's ok to continue" << endl; PutChar(0, 0); Send(0); ++current; // cerr << "Current is now " << current << endl; } else { PutChar(current, 0); Send(current); if (source == current) { current++; // cerr << "Current is now " << current << " sending ack" << endl; } else { // cerr << "Forwarding to " << current << " that dest is " << MyNodeId() << endl; } } } } // cerr << "My dest is " << my_dest << endl; } // Stage 5. void Stage5() { // my_dest = (MyNodeId() + 1) % NumberOfNodes(); if (MyNodeId() != 0) is_sink = !out_edge[my_dest]; // is_sink = my_dest == 0; // cerr << "Is sink " << is_sink << endl; } // Stage 6. // Cost: 100 for everyone except root. Total 100. // // Stage 7. // Cost: 100 for root. Total 0 (together with 7). void Stage67() { if (MyNodeId() == 0) { for (int i = 1; i < NumberOfNodes(); ++i) { PutChar(i, 0); Send(i); } } else { Receive(0); GetChar(0); } for (int i = 0; i < NumberOfCompanies(); ++i) prices.push_back(GetShareCost(i)); if (is_sink) { for (int i = 1; i < NumberOfNodes(); ++i) { int source = Receive(-1); // cerr << "Got prices from " << source << endl; GetChar(source); int companies = GetInt(source); while (companies--) prices.push_back(GetInt(source)); } for (int i = 0; i < NumberOfNodes(); ++i) if (i != MyNodeId()) { PutChar(i, 0); Send(i); } } else { // cerr << "Pushing prices to " << my_dest << endl; PutChar(my_dest, 1); PutInt(my_dest, prices.size()); for (int i = 0; i < prices.size(); ++i) PutInt(my_dest, prices[i]); Send(my_dest); while (true) { int source = Receive(-1); char valid = GetChar(source); // cerr << "Got a message from " << source << " valid: " << (valid != 0) << endl; if (valid == 0) break; PutChar(my_dest, 1); int companies = GetInt(source); PutInt(my_dest, companies); while (companies--) PutInt(my_dest, GetInt(source)); Send(my_dest); } } } // Stage 8. // Cost: 0. void Stage8() { if (is_sink) { sort(prices.rbegin(), prices.rend()); long long total = 0; for (int i = 0; i < prices.size(); ++i) { // cerr << prices[i] << endl; total += static_cast<long long>(prices[i]) * (i + 1); } cout << total << endl; } } int main() { Stage1(); Stage2(); Stage3(); Stage4(); Stage5(); Stage67(); Stage8(); 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 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 | // Protocol description. // // Stage 1. Each node finds its out edges. // Stage 2. Out edge information transferred to node 0. // Stage 3. Node 0 does planning who sends data which way. // Stage 4. Node 0 sends out orders. // Stage 5. Root gets notified. // Stage 6. Data gets sent. // Stage 7. Final node sends out halt signal to everyone else. // Stage 8. Final node prints out answer, everyone else halts. // // Total cost: 500+300+200+100+100=1200. #include "message.h" #include "skup.h" #include <algorithm> #include <iostream> #include <vector> using namespace std; bool is_sink = false; int my_dest = -1, sink = -1; vector<bool> in_edge, out_edge; vector<int> prices, dest; vector<vector<bool> > edge, reached; // Stage 1. // - Node 0 pokes smaller half of the remaining nodes. // - Each node either pokes back directly or via a twin. If poked directly, node 0 pokes the twin and waits for a poke-back. // - Repeat 3 times to cover all nodes. // - Repeat for all nodes. // Cost: 200 for chosen, 3 for other. Total 500. void Stage1() { in_edge.resize(NumberOfNodes()); out_edge.resize(NumberOfNodes()); for (int i = 0; i < NumberOfNodes(); ++i) { vector<int> other; for (int j = 1; j < NumberOfNodes(); ++j) other.push_back((i + j) % NumberOfNodes()); int unpaired = other.back(); other.resize(other.size() - 1); vector<int> group(NumberOfNodes()); vector<int> twin(NumberOfNodes()); for (int j = 0; j < other.size(); j += 2) { twin[other[j ]] = other[j + 1]; twin[other[j + 1]] = other[j ]; group[other[j ]] = 1; group[other[j + 1]] = 2; } if (MyNodeId() == i) { int wait; // cerr << "Source start" << endl; for (int j = 0; j < other.size(); j += 2) { PutChar(other[j], 1); Send(other[j]); } vector<int> pokes; for (int j = 0; j < other.size(); j += 2) { int source = Receive(-1); GetChar(source); if (group[source] == 1) { out_edge[source] = true; // cerr << "(group 1)->" << source << endl; pokes.push_back(twin[source]); } else { // cerr << "Negative message from " << source << endl; } } for (int i = 0; i < pokes.size(); ++i) { PutChar(pokes[i], 0); Send(pokes[i]); } // cerr << "Waiting for " << wait << " dummies" << endl; while (pokes.size() > 0) { int source = Receive(-1); // cerr << "Dummy message from " << source << endl; GetChar(source); pokes.resize(pokes.size() - 1); } // cerr << "Source finsihed handling group 1" << endl; for (int j = 1; j < other.size(); j += 2) { PutChar(other[j], 1); Send(other[j]); } for (int j = 0; j < other.size(); j += 2) { int source = Receive(-1); GetChar(source); if (group[source] == 2) { out_edge[source] = true; // cerr << "(group 2)->" << source << endl; pokes.push_back(twin[source]); } else { // cerr << "Negative message from " << source << endl; } } for (int i = 0; i < pokes.size(); ++i) { PutChar(pokes[i], 0); Send(pokes[i]); } // cerr << "Waiting for " << wait << " dummies" << endl; while (pokes.size() > 0) { int source = Receive(-1); // cerr << "Dummy message from " << source << endl; GetChar(source); pokes.resize(pokes.size() - 1); } // cerr << "Source finsihed handling group 2" << endl; PutChar(unpaired, 1); Send(unpaired); int source = Receive(-1); GetChar(source); if (source == unpaired) { out_edge[source] = true; // cerr << "(unpaired)->" << source << endl; PutChar(other[0], 0); Send(other[0]); Receive(other[0]); GetChar(other[0]); } // cerr << "Source done" << endl; } else if (MyNodeId() == unpaired) { // cerr << "Unpaired start" << endl; Receive(i); if (GetChar(i) == 1) { // cerr << "<-" << i << endl; in_edge[i] = true; PutChar(i, 0); Send(i); } else { // cerr << "Bumping to other[0]" << endl; PutChar(other[0], 0); Send(other[0]); } // cerr << "Unpaired done" << endl; } else if (group[MyNodeId()] == 1) { // cerr << "Group 1 start" << endl; Receive(i); if (GetChar(i) == 1) { in_edge[i] = true; // cerr << "<-" << i << endl; PutChar(i, 0); Send(i); } else { // cerr << "Bumping to twin" << endl; PutChar(twin[MyNodeId()], 0); Send(twin[MyNodeId()]); } int source = Receive(-1); // cerr << "Bump from " << source << endl; GetChar(source); PutChar(i, 0); Send(i); // cerr << "Group 1 done" << endl; if (MyNodeId() == other[0]) { // cerr << "Other[0] start" << endl; int source = Receive(-1); // cerr << "Bump from " << source << endl; GetChar(source); PutChar(i, 0); Send(i); // cerr << "Other[0] done" << endl; } } else { // cerr << "Group 2 start" << endl; int source = Receive(-1); // cerr << "Bump from " << source << endl; GetChar(source); PutChar(i, 0); Send(i); Receive(i); if (GetChar(i) == 1) { in_edge[i] = true; // cerr << "<-" << i << endl; PutChar(i, 0); Send(i); } else { // cerr << "Bumping to twin" << endl; PutChar(twin[MyNodeId()], 0); Send(twin[MyNodeId()]); } // cerr << "Group 2 done" << endl; } } } // Stage 2. // - Node 0 pokes node 1, gets poked back directly or via another node, indicating the number of out edges. // - Node 0 pokes node 1, gets poked back by out edges. // - Repeat for all nodes. // - Node 0 pokes everyone once again to mark the end. // Cost: 3 for 0, 100 for chosen, 2 for other. Total 300. void Stage2() { if (MyNodeId() == 0) { edge.resize(NumberOfNodes(), vector<bool>(NumberOfNodes())); for (int i = 0; i < NumberOfNodes(); ++i) if (out_edge[i]) edge[MyNodeId()][i] = true; for (int i = 0; i < NumberOfNodes(); ++i) if (in_edge[i]) edge[i][MyNodeId()] = true; for (int i = 1; i < NumberOfNodes(); ++i) { PutChar(i, 0); Send(i); // cerr << "Sent out request to " << i << endl; int out_count = Receive(-1); GetChar(out_count--); // cerr << "Received response with out_count = " << out_count << endl; PutChar(i, 0); Send(i); while (out_count--) { // cerr << "Requesting an edge " << endl; int source = Receive(-1); // cerr << "Received edge from " << source << endl; GetChar(source); edge[i][source] = true; } } for (int i = 1; i < NumberOfNodes(); ++i) { // cerr << "Pushing " << i << " past final stage" << endl; PutChar(i, 0); Send(i); } // cerr << "Source node done" << endl; } else { int stage = 0; while (stage < 3) { // cerr << "Stage " << stage << endl; int source; source = Receive(-1); GetChar(source); if (source != 0) { // cerr << "Forwaring information from " << source << endl; PutChar(0, 0); Send(0); continue; } if (stage == 0) { int out_count = 1; for (int j = 1; j < NumberOfNodes(); ++j) { if (out_edge[j]) { // cerr << "Counting edge " << MyNodeId() << "->" << j << endl; ++out_count; } } // cerr << "Sending back out_count = " << out_count << endl; if (out_count == MyNodeId()) { PutChar(0, 0); Send(0); } else { PutChar(out_count, 0); Send(out_count); } } else if (stage == 1) { for (int j = 1; j < NumberOfNodes(); ++j) if (out_edge[j]) { // cerr << "Sending back edge to " << j << endl; PutChar(j, 0); Send(j); } } ++stage; } } } void Stage3Dfs(const int a, const int b) { if (reached[a][b]) return; reached[a][b] = true; for (int i = 0; i < NumberOfNodes(); ++i) if (edge[b][i]) Stage3Dfs(a, i); } void Stage3ReverseDfs(const int a, const int b) { if (dest[a] >= 0) return; dest[a] = b; for (int i = 0; i < NumberOfNodes(); ++i) if (edge[i][a]) Stage3ReverseDfs(i, a); } // Stage 3. // - All in node 0's mind. void Stage3() { if (MyNodeId() == 0) { // for (int i = 0; i < NumberOfNodes(); ++i) for (int j = 0; j < NumberOfNodes(); ++j) if (edge[i][j]) cerr << i << "->" << j << endl; reached.resize(NumberOfNodes(), vector<bool>(NumberOfNodes())); for (int i = 0; i < NumberOfNodes(); ++i) Stage3Dfs(i, i); for (int i = 0; i < NumberOfNodes(); ++i) { bool all_in = true; bool all_out = true; for (int j = 0; j < NumberOfNodes(); ++j) if (!reached[j][i]) all_in = false; for (int j = 0; j < NumberOfNodes(); ++j) if (i != j && !edge[i][j]) all_out = false; if (all_in && !all_out) sink = i; } if (sink == -1) sink = 0; // cerr << "Sink " << sink << endl; dest.resize(NumberOfNodes(), -1); if (sink == 0) Stage3ReverseDfs(0, 0); else { int invalid_edge = -1; for (int i = 0; i < NumberOfNodes(); ++i) if (i != sink && !edge[sink][i]) invalid_edge = i; // cerr << invalid_edge << endl; Stage3ReverseDfs(sink, invalid_edge); } // for (int i = 0; i < NumberOfNodes(); ++i) if (i != sink) cerr << i << "->" << dest[i] << endl; my_dest = dest[0]; is_sink = sink == 0; } } // Stage 4. // - Node 1's destination pokes node 1, possibly poked by node 0 first. // - Node 1 notifies everyone that we're moving on to Node 2. // - Repeat for all nodes. // Cost: 1 for 0, 100 for chosen, 1 for other. Total 200. void Stage4() { if (MyNodeId() == 0) { for (int i = 1; i < NumberOfNodes(); ++i) { // cerr << "Notifying " << i << " of dest " << dest[i] << endl; if (dest[i] == 0) { PutChar(i, 0); Send(i); } else { PutChar(dest[i], 0); Send(dest[i]); } // cerr << "Waiting for ack from " << i << endl; Receive(i); GetChar(i); // cerr << "Ok to move on to next one" << endl; } } else { int current = 1; while (current < NumberOfNodes()) { int source = Receive(-1); GetChar(source); if (MyNodeId() == current) { // cerr << "Received my destination " << source << endl; my_dest = source; for (int i = 1; i < NumberOfNodes(); ++i) if (i != MyNodeId()) { // cerr << "Broadcasting update of current to " << i << endl; PutChar(i, 0); Send(i); } for (int i = 1; i < NumberOfNodes(); ++i) if (i != MyNodeId()) { // cerr << "Waiting for ack at " << i << endl; source = Receive(-1); GetChar(source); // cerr << "Got ack at " << i << endl; } // cerr << "Letting know source it's ok to continue" << endl; PutChar(0, 0); Send(0); ++current; // cerr << "Current is now " << current << endl; } else { PutChar(current, 0); Send(current); if (source == current) { current++; // cerr << "Current is now " << current << " sending ack" << endl; } else { // cerr << "Forwarding to " << current << " that dest is " << MyNodeId() << endl; } } } } // cerr << "My dest is " << my_dest << endl; } // Stage 5. void Stage5() { // my_dest = (MyNodeId() + 1) % NumberOfNodes(); if (MyNodeId() != 0) is_sink = !out_edge[my_dest]; // is_sink = my_dest == 0; // cerr << "Is sink " << is_sink << endl; } // Stage 6. // Cost: 100 for everyone except root. Total 100. // // Stage 7. // Cost: 100 for root. Total 0 (together with 7). void Stage67() { if (MyNodeId() == 0) { for (int i = 1; i < NumberOfNodes(); ++i) { PutChar(i, 0); Send(i); } } else { Receive(0); GetChar(0); } for (int i = 0; i < NumberOfCompanies(); ++i) prices.push_back(GetShareCost(i)); if (is_sink) { for (int i = 1; i < NumberOfNodes(); ++i) { int source = Receive(-1); // cerr << "Got prices from " << source << endl; GetChar(source); int companies = GetInt(source); while (companies--) prices.push_back(GetInt(source)); } for (int i = 0; i < NumberOfNodes(); ++i) if (i != MyNodeId()) { PutChar(i, 0); Send(i); } } else { // cerr << "Pushing prices to " << my_dest << endl; PutChar(my_dest, 1); PutInt(my_dest, prices.size()); for (int i = 0; i < prices.size(); ++i) PutInt(my_dest, prices[i]); Send(my_dest); while (true) { int source = Receive(-1); char valid = GetChar(source); // cerr << "Got a message from " << source << " valid: " << (valid != 0) << endl; if (valid == 0) break; PutChar(my_dest, 1); int companies = GetInt(source); PutInt(my_dest, companies); while (companies--) PutInt(my_dest, GetInt(source)); Send(my_dest); } } } // Stage 8. // Cost: 0. void Stage8() { if (is_sink) { sort(prices.rbegin(), prices.rend()); long long total = 0; for (int i = 0; i < prices.size(); ++i) { // cerr << prices[i] << endl; total += static_cast<long long>(prices[i]) * (i + 1); } cout << total << endl; } } int main() { Stage1(); Stage2(); Stage3(); Stage4(); Stage5(); Stage67(); Stage8(); return 0; } |