#include "message.h" #include "skup.h" /* long long NumberOfCompanies() { return 2; } long long GetShareCost(long long Id) { //assert(0 <= Id && Id < 2); return 100; } /// --- Wrappery do Put{Char, Int, LL} --- /// int __CanSend(int Src, int Dest) { if (Src == Dest) { return 1; } return Src % 2 != Dest % 2; // klika //return true; // sznurek //return Src + 1 == Dest; // klika z ogonem (wynik: 2500) //if(Src >= NumberOfNodes()/2 && Dest >= NumberOfNodes()/2) return true; //if(Src + 1 == Dest) return true; //return false; // klika z ogonem (wynik: 5575) //if(Src >= NumberOfNodes()/4 && Dest >= NumberOfNodes()/4) return true; //if(Src + 1 == Dest) return true; //return false; // kwadrat z ogonem (wynik: 9 + 10 + 10*9* ...) - program zwraca 819 //if(Src < 10 && Dest < 10) return Src + 1 == Dest; //if(Src >= 10 && Src/10 + 1 == Dest/10) return true; //if(Src == 9 && Dest/10 == 1) return true; //return false; // muszka (wynik: 99) //if(Src == 66 && Dest > 66) return true; //if(Src >= 33 && Src < 66 && Dest == 66) return true; //if(Src < 33 && Dest >= 33 && Dest < 66 && Src + 33 == Dest) return true; //return false; } void __PutCharWrapper(int Dest, char Value) { if (!__CanSend(MyNodeId(), Dest)) { Value = 0; } PutChar(Dest, Value); } void __PutIntWrapper(int Dest, int Value) { if (!__CanSend(MyNodeId(), Dest)) { Value = 0; } PutInt(Dest, Value); } void __PutLLWrapper(int Dest, long long Value) { if (!__CanSend(MyNodeId(), Dest)) { Value = 0; } PutLL(Dest, Value); } #define PutChar __PutCharWrapper #define PutInt __PutIntWrapper #define PutLL __PutLLWrapper /// -------------------------------------- /// */ #include <bits/stdc++.h> using namespace std; using ll = long long; using PII = pair<int,int>; int ri() { int x; scanf("%d",&x); return x; } ll rll() { ll x; scanf("%lld",&x); return x; } void print(int x) { printf("%d",x); } void print(ll x) { printf("%lld",x); } template<class T> void remin(T& var, T x) { var = min(var,x); } template<class T> void remax(T& var, T x) { var = max(var,x); } #define REP(i,n) for(int i=0; i<int(n); ++i) #define PER(i,n) for(int i=(n)-1; i>=0; --i) #define FOR(i,a,b) for(int i=(a); i<int(b); ++i) #define ROF(i,a,b) for(int i=(b)-1; i>=int(a); --i) #define ALL(c) (c).begin(), (c).end() ll randll() { ll r = 0; REP(i,5) { r ^= rand(); r <<= 15; } return abs(r); } ll rand(ll a, ll b) { return a + randll()%(b-a); } #define DBG false int NUM_NODES = NumberOfNodes(); const int ME = MyNodeId(); bool i_hear_them[100]; bool they_hear_me[100]; bool graph[100][100]; vector<pair<int,int>> edges_to_send; int num_edges_sent_to[100]; const int SEND_LIMIT = 1200 - 10; const int SEND_BYTES_LIMIT = 6 * 1024 * 1024; int total_bytes_sent = 0; int total_num_sends = 0; int num_sends[100]; void PUT_CHAR(int dest, char c) { total_bytes_sent += 1; PutChar(dest, c); } void PUT_INT(int dest, int i) { total_bytes_sent += 4; PutInt(dest, i); } void PUT_LL(int dest, int l) { total_bytes_sent += 8; PutLL(dest, l); } void SEND(int to) { if(DBG) { //fprintf(stderr, "\t\t\t\t\t\tSEND(%d)\t %d->%d\n", num_sends[to]++, ME, to); } // cancel send? if(total_num_sends >= SEND_LIMIT) { if(DBG) { fprintf(stderr, "already sent %d messages!", total_num_sends); abort(); } return; } ++total_num_sends; Send(to); } int num_recvs[100]; int RECEIVE(int want) { int from = Receive(want); //if(DBG) fprintf(stderr, "\t\t\t\t\t\tRECV(%d)\t\t %d->%d\n", num_recvs[from], from, ME); num_recvs[from]++; return from; } int main() { // test communication REP(curr, NUM_NODES) { //if(DBG) fprintf(stderr, "\nCURR %d\n", curr); vector<bool> is_a(NUM_NODES); { bool skip = false; REP(pair_a, NUM_NODES) if(pair_a != curr) { is_a[pair_a] = !skip; skip = !skip; } } auto next_el = [&](int a) { int pair_b = (a+1) % NUM_NODES; if(pair_b == curr) ++pair_b; pair_b %= NUM_NODES; return pair_b; }; auto prev_el = [&](int b) { int pair_a = (b + NUM_NODES-1) % NUM_NODES; if(pair_a == curr) --pair_a; if(pair_a < 0) pair_a += NUM_NODES; return pair_a; }; int fst = 0; if(curr == fst) ++fst; assert(is_a[fst]); int lst = NUM_NODES-1; if(curr == lst) --lst; vector<bool> need_fence_for(NUM_NODES); if(curr == ME) { // wait for signal if(curr > 0) { //if(DBG) fprintf(stderr, "wait for CONT from %d\n", curr-1); RECEIVE(curr-1); // CONT //if(DBG) fprintf(stderr, "CONT from %d\n", curr-1); } REP(el, NUM_NODES) if(el != curr && is_a[el] && el != lst) { PUT_CHAR(el, 1); // COMM SEND(el); } vector<bool> done(NUM_NODES); REP(rcv, NUM_NODES-2) { //if(DBG) fprintf(stderr, "wait for NOTI from\n"); int pair_x = RECEIVE(-1); // NOTI while(need_fence_for[pair_x]) { need_fence_for[pair_x] = false; pair_x = RECEIVE(-1); } assert(pair_x != ME); //if(DBG) fprintf(stderr, "NOTI from %d\n", pair_x); if(is_a[ pair_x ]) { int pair_a = pair_x; int pair_b = next_el(pair_a); if(done[ pair_a ]) { they_hear_me[pair_b] = false; // NOTI FORWARD FROM B assert(!done[pair_b]); done[pair_b] = true; } else { they_hear_me[pair_a] = true; // NOTI FROM A assert(!done[pair_a]); done[pair_a] = true; PUT_CHAR(pair_b, 1); SEND(pair_b); } } else { int pair_b = pair_x; int pair_a = pair_b - 1; if(pair_a == ME) --pair_a; if(done[ pair_a ]) { they_hear_me[ pair_b ] = true; // NOTI FROM B assert(!done[pair_b]); done[ pair_b ] = true; SEND( pair_a ); need_fence_for[pair_a] = true; //RECEIVE( pair_a ); } else { they_hear_me[ pair_a] = false; // NOTI FORWARD FROM A assert(!done[pair_a]); done[pair_a] = true; PUT_CHAR(pair_b, 1); SEND(pair_b); } } } } else if(is_a[ME] && ME != lst) { int b = next_el(ME); assert(ME < b); // no overflow //if(DBG) fprintf(stderr, "wait for COMM\n"); RECEIVE(curr); // COMM bool hear = GetChar(curr); i_hear_them[curr] = hear; if( hear ) { SEND(curr); // NOTI FROM A } else { SEND(b); // FORWARD VIA B } // swap //if(DBG) fprintf(stderr, "wait for FORWARD_VIA_A or NOTI_DUMMY\n"); int from = RECEIVE(-1); // FORWARD VIA A or NOTI DUMMY //if(DBG) fprintf(stderr, "SIGNAL from %d\n", from); assert(from == b || from == curr); //if(from == b) { SEND(curr); // NOTI FORWARD FROM B or NOTI END //} } else if(!is_a[ME] && ME != lst) { // is b int a = prev_el(ME); assert(a < ME); // no overflow //if(DBG) fprintf(stderr, "wait for left or curr (COMM)\n"); int from = RECEIVE(-1); assert(from == a || from == curr); //if(DBG) fprintf(stderr, "SIGNAL from %d\n", from); if( from == a ) { SEND(curr); // NOTI FORWARD FROM A //if(DBG) fprintf(stderr, "wait for COMM\n"); RECEIVE(curr); } // swap bool hear = GetChar(curr); //if(DBG) fprintf(stderr, "hear from %d: %d\n", curr, (int)hear); i_hear_them[curr] = hear; if( hear ) { SEND(curr); // NOTI FROM B } else { SEND(a); // FORWARD VIA A } } // last element without pair... int pair_a = lst; int pair_b = fst; if(ME == curr) { if(need_fence_for[pair_b]) { need_fence_for[pair_b] = false; RECEIVE(pair_b); } PUT_CHAR(pair_a, 1); SEND(pair_a); //if(DBG) fprintf(stderr, "wait for last element NOTI\n"); int from = RECEIVE(-1); while(need_fence_for[from]) { need_fence_for[from] = false; from = RECEIVE(-1); } assert(from == pair_a || from == pair_b); if(from == pair_a) { they_hear_me[ pair_a ] = true; //done[ pair_a ] = true; SEND(pair_b); // wait! //need_fence_for[pair_b] = true; RECEIVE(pair_b); } else{ they_hear_me[ pair_a ] = false; //done[ pair_a ] = true; } } else if(ME == pair_a) { //if(DBG) fprintf(stderr, "wait for COMM\n"); RECEIVE(curr); bool hear = GetChar(curr); i_hear_them[curr] = hear; if( hear ) { SEND(curr); // NOTI FROM A } else { SEND(pair_b); // FORWARD VIA B } } else if(ME == pair_b) { //if(DBG) fprintf(stderr, "wait to make some last help\n"); int from = RECEIVE(-1); //if(DBG) if(from != pair_a && from != curr) fprintf(stderr, "received from %d!\n", from); assert(from == pair_a || from == curr); //if(DBG) fprintf(stderr, "last help done. pinging curr anyway\n"); SEND(curr); // NOTI FORWARD FROM A or NOTI END } if(ME == curr) { REP(i,NUM_NODES) if(need_fence_for[i]) RECEIVE(i); } // notify next boss if(ME == curr) { if(curr < NUM_NODES-1) { SEND(curr+1); // CONT } else { // the end! // who to notify next? } } } //int how_many = 0; /* if(DBG) { fprintf(stderr, "they_hear_me: "); REP(i, NUM_NODES) if(they_hear_me[i]) fprintf(stderr, "%d ", i), ++how_many; fprintf(stderr, "\n"); } */ /* REP(i, NUM_NODES) if(i != ME){ bool should_be = i%2 != ME%2; //if(ME == NUM_NODES-1 && i == NUM_NODES-2) should_be = !should_be; assert(they_hear_me[i] == should_be); } */ //if(DBG) fprintf(stderr, "messages sent: %d (%d KB)\n", total_num_sends, total_sent/1024); // receive fence from NUM_NODES-1? if(ME == NUM_NODES - 1) { REP(i, NUM_NODES-1) SEND(i); } else { RECEIVE(NUM_NODES-1); } REP(i, NUM_NODES) if(i != ME) { if(i_hear_them[i]) { edges_to_send.push_back({i, ME}); graph[i][ME] = true; } if(they_hear_me[i]) { edges_to_send.push_back({ME, i}); graph[ME][i] = true; } } vector<pair<int,int>> pairs; REP(i, NUM_NODES) { REP(j, NUM_NODES) { if(i == j) continue; pairs.push_back({i,j}); } } srand(69); // debug num send and recv //if(DBG) { // fprintf(stderr, "SENT %d, RECV ", total_num_sends); // REP(i,NUM_NODES-1) fprintf(stderr, "(%d: %d) ", i, num_recvs[i]); // fprintf(stderr, "\n"); //} vector<bool> block_recv_from(NUM_NODES); vector<bool> block_send_to(NUM_NODES); vector<int> already_received_from(NUM_NODES); int iter_fail = -1; REP(iter, 1000) { random_shuffle(ALL(pairs)); //if(DBG) fprintf(stderr, "\nITER %d\n", iter); vector<bool> already_sent_to(NUM_NODES); auto try_send_to = [&](int dest) { if(!they_hear_me[dest]) return; if(already_sent_to[dest]) return; if(block_send_to[dest]) { //if(DBG) fprintf(stderr, "ignoring send %d->%d above limit\n", ME, dest); return; } PUT_CHAR(dest, 69); if(total_num_sends >= SEND_LIMIT - NUM_NODES*3 - 5 || total_bytes_sent > SEND_BYTES_LIMIT) { PUT_INT(dest, INT_MAX); block_send_to[dest] = true; if(iter_fail == -1) iter_fail = iter; //if(DBG) fprintf(stderr, "putting INT_MAX to %d (reached %d messages, %d KB), iter==%d\n", // dest, total_num_sends, total_bytes_sent/1024, iter); } else { // send PUT_INT(dest, edges_to_send.size() - num_edges_sent_to[dest]); FOR(i, num_edges_sent_to[dest], edges_to_send.size()) { PUT_CHAR(dest, edges_to_send[i].first); PUT_CHAR(dest, edges_to_send[i].second); } num_edges_sent_to[dest] = edges_to_send.size(); } //if(DBG) fprintf(stderr, "SEND %d->%d\n", ME, dest); SEND(dest); already_sent_to[dest] = true; }; for(auto p : pairs) { if(ME == p.first) { int dest = p.second; try_send_to(dest); } if(ME == p.second && i_hear_them[p.first] && !block_recv_from[p.first]) { while(already_received_from[p.first] == 0) { //if(DBG) fprintf(stderr, "RECEIVE ?->%d (hoping for %d)\n", ME, p.first); int from = RECEIVE(-1); already_received_from[from]++; char type = GetChar(from); //assert(type == 69); if(type != 69) { goto dead; } int num_data = GetInt(from); //if(DBG) fprintf(stderr, "received from %d (%d)\n", from, num_data); if(num_data == INT_MAX) { assert(!block_recv_from[from]); block_recv_from[from] = true; continue; } REP(i, num_data) { int a = GetChar(from); int b = GetChar(from); if(graph[a][b]) continue; graph[a][b] = true; edges_to_send.push_back({a,b}); } // propagate REP(i, NUM_NODES) if(i != ME && they_hear_me[i]) { if(num_edges_sent_to[i] < (int)edges_to_send.size()) { if(DBG) if(total_num_sends >= SEND_LIMIT) { fprintf(stderr, "unable to send edge (%d,%d) from %d to %d\n", edges_to_send.back().first, edges_to_send.back().second, ME, i); assert(total_num_sends < SEND_LIMIT); } try_send_to(i); } } } } } REP(node, NUM_NODES) already_received_from[node]--; } dead: REP(node, NUM_NODES) if(node != ME) if(they_hear_me[node]) { PUT_CHAR(node, 42); SEND(node); PUT_CHAR(node, 43); SEND(node); } REP(node, NUM_NODES) if(node != ME) if(i_hear_them[node]) { RECEIVE(node); int r = GetChar(node); while(r == 69) { int num_data = GetInt(node); if(num_data == INT_MAX) num_data = 0; REP(i, num_data) { GetChar(node); GetChar(node); } RECEIVE(node); r = GetChar(node); } if(r == 43) continue; RECEIVE(node); r = GetChar(node); assert(r == 43); } if(DBG) if(ME % 10 == 0) fprintf(stderr, "iter_fail == %d\n", iter_fail); // now all data should have been propagated... bool path_exists[100][100]; REP(fr, NUM_NODES) REP(to, NUM_NODES) { path_exists[fr][to] = graph[fr][to]; } REP(i, NUM_NODES) path_exists[i][i] = true; REP(iter, 8) { REP(fr, NUM_NODES) REP(to, NUM_NODES) REP(by, NUM_NODES) { path_exists[fr][to] |= path_exists[fr][by] && path_exists[by][to]; } } bool i_have_all = true; REP(i, NUM_NODES) if(!path_exists[i][ME]) i_have_all = false; int local_boss = ME; REP(i, NUM_NODES) if(i < local_boss && path_exists[ME][i]) local_boss = i; set<int> cities; vector<int> values; std::vector<int> values_per_city[NUM_NODES]; cities.insert(ME); { int num_values = NumberOfCompanies(); REP(i,num_values) { int share_cost = GetShareCost(i); values.push_back(share_cost); values_per_city[ME].push_back(share_cost); } } vector<int> layers(NUM_NODES); REP(i,NUM_NODES) layers[i] = INT_MAX; layers[local_boss] = 0; deque<int> deq; deq.push_back(local_boss); while(deq.size()) { int c = deq.front(); deq.pop_front(); REP(dest, NUM_NODES) if(graph[dest][c] && dest != c) { if(layers[dest] != INT_MAX) continue; layers[dest] = layers[c] + 1; deq.push_back(dest); } } if(DBG) { fprintf(stderr, "layers: ");; REP(i, NUM_NODES) fprintf(stderr, "%d ", layers[i]); fprintf(stderr, "\n"); } // collect from lower layers REP(from, NUM_NODES) { if(from == ME) continue; if(i_hear_them[from] == false) continue; if(path_exists[from][ME] == false) continue; if(layers[from] - 1 != layers[ME]) continue; if(DBG) fprintf(stderr, "collect from %d\n", from); RECEIVE(from); char code = GetChar(from); assert(code == 70); int num_cities = GetChar(from); REP(ic, num_cities) { int c = GetChar(from); bool have = cities.find(c) != cities.end(); int num_values = GetInt(from); REP(iv, num_values) { int v = GetInt(from); if(!have) { values.push_back(v); values_per_city[c].push_back(v); } } cities.insert(c); } } if(DBG) fprintf(stderr, "local_boss: %d\n", local_boss); if(local_boss == ME && i_have_all) { if(DBG) fprintf(stderr, "root: collected %d edges\n", (int)edges_to_send.size()); if(DBG) fprintf(stderr, "root: collected %d cities\n", (int)cities.size()); assert((int)cities.size() == NUM_NODES); // print solution ll result = 0; std::sort(ALL(values)); ll mno = values.size(); for(auto v : values) { result += mno * v; --mno; } assert(mno == 0); printf("%lld\n", result); } else { // send to upper layer bool once = true; REP(node, NUM_NODES) if(node != ME) { if(they_hear_me[node] == false) continue; if(path_exists[node][ME] && layers[ME] != layers[node]+1) continue; if(once) { once = false; PUT_CHAR(node, 70); // send once int num_cities = cities.size(); PUT_CHAR(node, num_cities); REP(c, NUM_NODES) { if(values_per_city[c].empty()) continue; PUT_CHAR(node, c); PUT_INT(node, values_per_city[c].size()); for(auto v : values_per_city[c]) { PUT_INT(node, v); } } if(DBG) fprintf(stderr, "send to %d\n", node); SEND(node); } else { if(DBG) fprintf(stderr, "send to %d\n", node); // send crap PUT_CHAR(node, 70); PUT_CHAR(node, 0); SEND(node); } } } if(DBG) if(ME==NUM_NODES-1) { fprintf(stderr, "EDGES(%d): ", (int)edges_to_send.size()); REP(y,NUM_NODES) REP(x,NUM_NODES) { if(graph[y][x]) { //fprintf(stderr, "(%d->%d) ", y, x); } } fprintf(stderr, "\n"); } if(DBG) if(ME == NUM_NODES/2) fprintf(stderr, "messages sent: %d (%d KB)\n", total_num_sends, total_bytes_sent/1024); 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 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 | #include "message.h" #include "skup.h" /* long long NumberOfCompanies() { return 2; } long long GetShareCost(long long Id) { //assert(0 <= Id && Id < 2); return 100; } /// --- Wrappery do Put{Char, Int, LL} --- /// int __CanSend(int Src, int Dest) { if (Src == Dest) { return 1; } return Src % 2 != Dest % 2; // klika //return true; // sznurek //return Src + 1 == Dest; // klika z ogonem (wynik: 2500) //if(Src >= NumberOfNodes()/2 && Dest >= NumberOfNodes()/2) return true; //if(Src + 1 == Dest) return true; //return false; // klika z ogonem (wynik: 5575) //if(Src >= NumberOfNodes()/4 && Dest >= NumberOfNodes()/4) return true; //if(Src + 1 == Dest) return true; //return false; // kwadrat z ogonem (wynik: 9 + 10 + 10*9* ...) - program zwraca 819 //if(Src < 10 && Dest < 10) return Src + 1 == Dest; //if(Src >= 10 && Src/10 + 1 == Dest/10) return true; //if(Src == 9 && Dest/10 == 1) return true; //return false; // muszka (wynik: 99) //if(Src == 66 && Dest > 66) return true; //if(Src >= 33 && Src < 66 && Dest == 66) return true; //if(Src < 33 && Dest >= 33 && Dest < 66 && Src + 33 == Dest) return true; //return false; } void __PutCharWrapper(int Dest, char Value) { if (!__CanSend(MyNodeId(), Dest)) { Value = 0; } PutChar(Dest, Value); } void __PutIntWrapper(int Dest, int Value) { if (!__CanSend(MyNodeId(), Dest)) { Value = 0; } PutInt(Dest, Value); } void __PutLLWrapper(int Dest, long long Value) { if (!__CanSend(MyNodeId(), Dest)) { Value = 0; } PutLL(Dest, Value); } #define PutChar __PutCharWrapper #define PutInt __PutIntWrapper #define PutLL __PutLLWrapper /// -------------------------------------- /// */ #include <bits/stdc++.h> using namespace std; using ll = long long; using PII = pair<int,int>; int ri() { int x; scanf("%d",&x); return x; } ll rll() { ll x; scanf("%lld",&x); return x; } void print(int x) { printf("%d",x); } void print(ll x) { printf("%lld",x); } template<class T> void remin(T& var, T x) { var = min(var,x); } template<class T> void remax(T& var, T x) { var = max(var,x); } #define REP(i,n) for(int i=0; i<int(n); ++i) #define PER(i,n) for(int i=(n)-1; i>=0; --i) #define FOR(i,a,b) for(int i=(a); i<int(b); ++i) #define ROF(i,a,b) for(int i=(b)-1; i>=int(a); --i) #define ALL(c) (c).begin(), (c).end() ll randll() { ll r = 0; REP(i,5) { r ^= rand(); r <<= 15; } return abs(r); } ll rand(ll a, ll b) { return a + randll()%(b-a); } #define DBG false int NUM_NODES = NumberOfNodes(); const int ME = MyNodeId(); bool i_hear_them[100]; bool they_hear_me[100]; bool graph[100][100]; vector<pair<int,int>> edges_to_send; int num_edges_sent_to[100]; const int SEND_LIMIT = 1200 - 10; const int SEND_BYTES_LIMIT = 6 * 1024 * 1024; int total_bytes_sent = 0; int total_num_sends = 0; int num_sends[100]; void PUT_CHAR(int dest, char c) { total_bytes_sent += 1; PutChar(dest, c); } void PUT_INT(int dest, int i) { total_bytes_sent += 4; PutInt(dest, i); } void PUT_LL(int dest, int l) { total_bytes_sent += 8; PutLL(dest, l); } void SEND(int to) { if(DBG) { //fprintf(stderr, "\t\t\t\t\t\tSEND(%d)\t %d->%d\n", num_sends[to]++, ME, to); } // cancel send? if(total_num_sends >= SEND_LIMIT) { if(DBG) { fprintf(stderr, "already sent %d messages!", total_num_sends); abort(); } return; } ++total_num_sends; Send(to); } int num_recvs[100]; int RECEIVE(int want) { int from = Receive(want); //if(DBG) fprintf(stderr, "\t\t\t\t\t\tRECV(%d)\t\t %d->%d\n", num_recvs[from], from, ME); num_recvs[from]++; return from; } int main() { // test communication REP(curr, NUM_NODES) { //if(DBG) fprintf(stderr, "\nCURR %d\n", curr); vector<bool> is_a(NUM_NODES); { bool skip = false; REP(pair_a, NUM_NODES) if(pair_a != curr) { is_a[pair_a] = !skip; skip = !skip; } } auto next_el = [&](int a) { int pair_b = (a+1) % NUM_NODES; if(pair_b == curr) ++pair_b; pair_b %= NUM_NODES; return pair_b; }; auto prev_el = [&](int b) { int pair_a = (b + NUM_NODES-1) % NUM_NODES; if(pair_a == curr) --pair_a; if(pair_a < 0) pair_a += NUM_NODES; return pair_a; }; int fst = 0; if(curr == fst) ++fst; assert(is_a[fst]); int lst = NUM_NODES-1; if(curr == lst) --lst; vector<bool> need_fence_for(NUM_NODES); if(curr == ME) { // wait for signal if(curr > 0) { //if(DBG) fprintf(stderr, "wait for CONT from %d\n", curr-1); RECEIVE(curr-1); // CONT //if(DBG) fprintf(stderr, "CONT from %d\n", curr-1); } REP(el, NUM_NODES) if(el != curr && is_a[el] && el != lst) { PUT_CHAR(el, 1); // COMM SEND(el); } vector<bool> done(NUM_NODES); REP(rcv, NUM_NODES-2) { //if(DBG) fprintf(stderr, "wait for NOTI from\n"); int pair_x = RECEIVE(-1); // NOTI while(need_fence_for[pair_x]) { need_fence_for[pair_x] = false; pair_x = RECEIVE(-1); } assert(pair_x != ME); //if(DBG) fprintf(stderr, "NOTI from %d\n", pair_x); if(is_a[ pair_x ]) { int pair_a = pair_x; int pair_b = next_el(pair_a); if(done[ pair_a ]) { they_hear_me[pair_b] = false; // NOTI FORWARD FROM B assert(!done[pair_b]); done[pair_b] = true; } else { they_hear_me[pair_a] = true; // NOTI FROM A assert(!done[pair_a]); done[pair_a] = true; PUT_CHAR(pair_b, 1); SEND(pair_b); } } else { int pair_b = pair_x; int pair_a = pair_b - 1; if(pair_a == ME) --pair_a; if(done[ pair_a ]) { they_hear_me[ pair_b ] = true; // NOTI FROM B assert(!done[pair_b]); done[ pair_b ] = true; SEND( pair_a ); need_fence_for[pair_a] = true; //RECEIVE( pair_a ); } else { they_hear_me[ pair_a] = false; // NOTI FORWARD FROM A assert(!done[pair_a]); done[pair_a] = true; PUT_CHAR(pair_b, 1); SEND(pair_b); } } } } else if(is_a[ME] && ME != lst) { int b = next_el(ME); assert(ME < b); // no overflow //if(DBG) fprintf(stderr, "wait for COMM\n"); RECEIVE(curr); // COMM bool hear = GetChar(curr); i_hear_them[curr] = hear; if( hear ) { SEND(curr); // NOTI FROM A } else { SEND(b); // FORWARD VIA B } // swap //if(DBG) fprintf(stderr, "wait for FORWARD_VIA_A or NOTI_DUMMY\n"); int from = RECEIVE(-1); // FORWARD VIA A or NOTI DUMMY //if(DBG) fprintf(stderr, "SIGNAL from %d\n", from); assert(from == b || from == curr); //if(from == b) { SEND(curr); // NOTI FORWARD FROM B or NOTI END //} } else if(!is_a[ME] && ME != lst) { // is b int a = prev_el(ME); assert(a < ME); // no overflow //if(DBG) fprintf(stderr, "wait for left or curr (COMM)\n"); int from = RECEIVE(-1); assert(from == a || from == curr); //if(DBG) fprintf(stderr, "SIGNAL from %d\n", from); if( from == a ) { SEND(curr); // NOTI FORWARD FROM A //if(DBG) fprintf(stderr, "wait for COMM\n"); RECEIVE(curr); } // swap bool hear = GetChar(curr); //if(DBG) fprintf(stderr, "hear from %d: %d\n", curr, (int)hear); i_hear_them[curr] = hear; if( hear ) { SEND(curr); // NOTI FROM B } else { SEND(a); // FORWARD VIA A } } // last element without pair... int pair_a = lst; int pair_b = fst; if(ME == curr) { if(need_fence_for[pair_b]) { need_fence_for[pair_b] = false; RECEIVE(pair_b); } PUT_CHAR(pair_a, 1); SEND(pair_a); //if(DBG) fprintf(stderr, "wait for last element NOTI\n"); int from = RECEIVE(-1); while(need_fence_for[from]) { need_fence_for[from] = false; from = RECEIVE(-1); } assert(from == pair_a || from == pair_b); if(from == pair_a) { they_hear_me[ pair_a ] = true; //done[ pair_a ] = true; SEND(pair_b); // wait! //need_fence_for[pair_b] = true; RECEIVE(pair_b); } else{ they_hear_me[ pair_a ] = false; //done[ pair_a ] = true; } } else if(ME == pair_a) { //if(DBG) fprintf(stderr, "wait for COMM\n"); RECEIVE(curr); bool hear = GetChar(curr); i_hear_them[curr] = hear; if( hear ) { SEND(curr); // NOTI FROM A } else { SEND(pair_b); // FORWARD VIA B } } else if(ME == pair_b) { //if(DBG) fprintf(stderr, "wait to make some last help\n"); int from = RECEIVE(-1); //if(DBG) if(from != pair_a && from != curr) fprintf(stderr, "received from %d!\n", from); assert(from == pair_a || from == curr); //if(DBG) fprintf(stderr, "last help done. pinging curr anyway\n"); SEND(curr); // NOTI FORWARD FROM A or NOTI END } if(ME == curr) { REP(i,NUM_NODES) if(need_fence_for[i]) RECEIVE(i); } // notify next boss if(ME == curr) { if(curr < NUM_NODES-1) { SEND(curr+1); // CONT } else { // the end! // who to notify next? } } } //int how_many = 0; /* if(DBG) { fprintf(stderr, "they_hear_me: "); REP(i, NUM_NODES) if(they_hear_me[i]) fprintf(stderr, "%d ", i), ++how_many; fprintf(stderr, "\n"); } */ /* REP(i, NUM_NODES) if(i != ME){ bool should_be = i%2 != ME%2; //if(ME == NUM_NODES-1 && i == NUM_NODES-2) should_be = !should_be; assert(they_hear_me[i] == should_be); } */ //if(DBG) fprintf(stderr, "messages sent: %d (%d KB)\n", total_num_sends, total_sent/1024); // receive fence from NUM_NODES-1? if(ME == NUM_NODES - 1) { REP(i, NUM_NODES-1) SEND(i); } else { RECEIVE(NUM_NODES-1); } REP(i, NUM_NODES) if(i != ME) { if(i_hear_them[i]) { edges_to_send.push_back({i, ME}); graph[i][ME] = true; } if(they_hear_me[i]) { edges_to_send.push_back({ME, i}); graph[ME][i] = true; } } vector<pair<int,int>> pairs; REP(i, NUM_NODES) { REP(j, NUM_NODES) { if(i == j) continue; pairs.push_back({i,j}); } } srand(69); // debug num send and recv //if(DBG) { // fprintf(stderr, "SENT %d, RECV ", total_num_sends); // REP(i,NUM_NODES-1) fprintf(stderr, "(%d: %d) ", i, num_recvs[i]); // fprintf(stderr, "\n"); //} vector<bool> block_recv_from(NUM_NODES); vector<bool> block_send_to(NUM_NODES); vector<int> already_received_from(NUM_NODES); int iter_fail = -1; REP(iter, 1000) { random_shuffle(ALL(pairs)); //if(DBG) fprintf(stderr, "\nITER %d\n", iter); vector<bool> already_sent_to(NUM_NODES); auto try_send_to = [&](int dest) { if(!they_hear_me[dest]) return; if(already_sent_to[dest]) return; if(block_send_to[dest]) { //if(DBG) fprintf(stderr, "ignoring send %d->%d above limit\n", ME, dest); return; } PUT_CHAR(dest, 69); if(total_num_sends >= SEND_LIMIT - NUM_NODES*3 - 5 || total_bytes_sent > SEND_BYTES_LIMIT) { PUT_INT(dest, INT_MAX); block_send_to[dest] = true; if(iter_fail == -1) iter_fail = iter; //if(DBG) fprintf(stderr, "putting INT_MAX to %d (reached %d messages, %d KB), iter==%d\n", // dest, total_num_sends, total_bytes_sent/1024, iter); } else { // send PUT_INT(dest, edges_to_send.size() - num_edges_sent_to[dest]); FOR(i, num_edges_sent_to[dest], edges_to_send.size()) { PUT_CHAR(dest, edges_to_send[i].first); PUT_CHAR(dest, edges_to_send[i].second); } num_edges_sent_to[dest] = edges_to_send.size(); } //if(DBG) fprintf(stderr, "SEND %d->%d\n", ME, dest); SEND(dest); already_sent_to[dest] = true; }; for(auto p : pairs) { if(ME == p.first) { int dest = p.second; try_send_to(dest); } if(ME == p.second && i_hear_them[p.first] && !block_recv_from[p.first]) { while(already_received_from[p.first] == 0) { //if(DBG) fprintf(stderr, "RECEIVE ?->%d (hoping for %d)\n", ME, p.first); int from = RECEIVE(-1); already_received_from[from]++; char type = GetChar(from); //assert(type == 69); if(type != 69) { goto dead; } int num_data = GetInt(from); //if(DBG) fprintf(stderr, "received from %d (%d)\n", from, num_data); if(num_data == INT_MAX) { assert(!block_recv_from[from]); block_recv_from[from] = true; continue; } REP(i, num_data) { int a = GetChar(from); int b = GetChar(from); if(graph[a][b]) continue; graph[a][b] = true; edges_to_send.push_back({a,b}); } // propagate REP(i, NUM_NODES) if(i != ME && they_hear_me[i]) { if(num_edges_sent_to[i] < (int)edges_to_send.size()) { if(DBG) if(total_num_sends >= SEND_LIMIT) { fprintf(stderr, "unable to send edge (%d,%d) from %d to %d\n", edges_to_send.back().first, edges_to_send.back().second, ME, i); assert(total_num_sends < SEND_LIMIT); } try_send_to(i); } } } } } REP(node, NUM_NODES) already_received_from[node]--; } dead: REP(node, NUM_NODES) if(node != ME) if(they_hear_me[node]) { PUT_CHAR(node, 42); SEND(node); PUT_CHAR(node, 43); SEND(node); } REP(node, NUM_NODES) if(node != ME) if(i_hear_them[node]) { RECEIVE(node); int r = GetChar(node); while(r == 69) { int num_data = GetInt(node); if(num_data == INT_MAX) num_data = 0; REP(i, num_data) { GetChar(node); GetChar(node); } RECEIVE(node); r = GetChar(node); } if(r == 43) continue; RECEIVE(node); r = GetChar(node); assert(r == 43); } if(DBG) if(ME % 10 == 0) fprintf(stderr, "iter_fail == %d\n", iter_fail); // now all data should have been propagated... bool path_exists[100][100]; REP(fr, NUM_NODES) REP(to, NUM_NODES) { path_exists[fr][to] = graph[fr][to]; } REP(i, NUM_NODES) path_exists[i][i] = true; REP(iter, 8) { REP(fr, NUM_NODES) REP(to, NUM_NODES) REP(by, NUM_NODES) { path_exists[fr][to] |= path_exists[fr][by] && path_exists[by][to]; } } bool i_have_all = true; REP(i, NUM_NODES) if(!path_exists[i][ME]) i_have_all = false; int local_boss = ME; REP(i, NUM_NODES) if(i < local_boss && path_exists[ME][i]) local_boss = i; set<int> cities; vector<int> values; std::vector<int> values_per_city[NUM_NODES]; cities.insert(ME); { int num_values = NumberOfCompanies(); REP(i,num_values) { int share_cost = GetShareCost(i); values.push_back(share_cost); values_per_city[ME].push_back(share_cost); } } vector<int> layers(NUM_NODES); REP(i,NUM_NODES) layers[i] = INT_MAX; layers[local_boss] = 0; deque<int> deq; deq.push_back(local_boss); while(deq.size()) { int c = deq.front(); deq.pop_front(); REP(dest, NUM_NODES) if(graph[dest][c] && dest != c) { if(layers[dest] != INT_MAX) continue; layers[dest] = layers[c] + 1; deq.push_back(dest); } } if(DBG) { fprintf(stderr, "layers: ");; REP(i, NUM_NODES) fprintf(stderr, "%d ", layers[i]); fprintf(stderr, "\n"); } // collect from lower layers REP(from, NUM_NODES) { if(from == ME) continue; if(i_hear_them[from] == false) continue; if(path_exists[from][ME] == false) continue; if(layers[from] - 1 != layers[ME]) continue; if(DBG) fprintf(stderr, "collect from %d\n", from); RECEIVE(from); char code = GetChar(from); assert(code == 70); int num_cities = GetChar(from); REP(ic, num_cities) { int c = GetChar(from); bool have = cities.find(c) != cities.end(); int num_values = GetInt(from); REP(iv, num_values) { int v = GetInt(from); if(!have) { values.push_back(v); values_per_city[c].push_back(v); } } cities.insert(c); } } if(DBG) fprintf(stderr, "local_boss: %d\n", local_boss); if(local_boss == ME && i_have_all) { if(DBG) fprintf(stderr, "root: collected %d edges\n", (int)edges_to_send.size()); if(DBG) fprintf(stderr, "root: collected %d cities\n", (int)cities.size()); assert((int)cities.size() == NUM_NODES); // print solution ll result = 0; std::sort(ALL(values)); ll mno = values.size(); for(auto v : values) { result += mno * v; --mno; } assert(mno == 0); printf("%lld\n", result); } else { // send to upper layer bool once = true; REP(node, NUM_NODES) if(node != ME) { if(they_hear_me[node] == false) continue; if(path_exists[node][ME] && layers[ME] != layers[node]+1) continue; if(once) { once = false; PUT_CHAR(node, 70); // send once int num_cities = cities.size(); PUT_CHAR(node, num_cities); REP(c, NUM_NODES) { if(values_per_city[c].empty()) continue; PUT_CHAR(node, c); PUT_INT(node, values_per_city[c].size()); for(auto v : values_per_city[c]) { PUT_INT(node, v); } } if(DBG) fprintf(stderr, "send to %d\n", node); SEND(node); } else { if(DBG) fprintf(stderr, "send to %d\n", node); // send crap PUT_CHAR(node, 70); PUT_CHAR(node, 0); SEND(node); } } } if(DBG) if(ME==NUM_NODES-1) { fprintf(stderr, "EDGES(%d): ", (int)edges_to_send.size()); REP(y,NUM_NODES) REP(x,NUM_NODES) { if(graph[y][x]) { //fprintf(stderr, "(%d->%d) ", y, x); } } fprintf(stderr, "\n"); } if(DBG) if(ME == NUM_NODES/2) fprintf(stderr, "messages sent: %d (%d KB)\n", total_num_sends, total_bytes_sent/1024); return 0; } |