QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#660063#2946. Abridged Readingenze114514#AC ✓112ms3968kbC++203.8kb2024-10-20 04:05:222024-10-20 04:05:22

Judging History

你现在查看的是最新测评结果

  • [2024-10-20 04:05:22]
  • 评测
  • 测评结果:AC
  • 用时:112ms
  • 内存:3968kb
  • [2024-10-20 04:05:22]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;

#define pb push_back

const ld pi = 3.14159265358979323846;
const int mod = 998244353;
const ll INF = 1e18;

template<typename T>
T chmax(T a, T b) {
    return a > b ? a : b;
}

template<typename T>
T chmin(T a, T b) {
    return a > b ? b : a;
}

const int N = (int)1e5 + 1, M = N * 2;

struct LCA{
    vector<int> h, w, e, ne, dep;
    vector<vector<int>> fa;
    int n, idx;

    const int depth = 18;

    LCA(int _n){
        n = _n;
        idx = 0;

        dep.resize(n, (int)1e9);
        fa.resize(n);

        ne.resize(n * 2, 0);
        h.resize(n * 2, -1);
        e.resize(n * 2, 0);

        for(int i = 0; i < n; i++){
            fa[i] = vector<int>(depth + 1);
        }
    }

    void add(int u, int v) {
        e[idx] = v, ne[idx] = h[u], h[u] = idx++;
        e[idx] = u, ne[idx] = h[v], h[v] = idx++;
    }

    void bfs(int rt) {
        dep[rt] = 1;
        dep[0] = 0;

        queue<int> q;
        q.push(rt);

        while(!q.empty()){
            int u = q.front();
            q.pop();
            for(int i = h[u]; i != -1; i = ne[i]){
                int j = e[i];
                if(dep[j] > dep[u] + 1){
                    dep[j] = dep[u] + 1;

                    q.push(j);
                    fa[j][0] = u;
                    for(int k = 1; k < depth; k++){
                        fa[j][k] = fa[fa[j][k - 1]][k - 1];
                    }
                }
            }
        }
    }

    int lca(int u, int v){
        if(dep[u] < dep[v]) swap(u, v);

        for(int i = depth - 1; i >= 0; i--){
            if(dep[fa[u][i]] >= dep[v]){
                u = fa[u][i];
            }
        }
        if(u == v) return u;

        for(int i = depth - 1; i >= 0; i--){
            if (fa[u][i] != fa[v][i]){
                u = fa[u][i];
                v = fa[v][i];
            }
        }
        return fa[u][0];
    }
};

int h[N], e[M], ne[M], idx;

void add(int u, int v) {
    e[idx] = v, ne[idx] = h[u], h[u] = idx++;
}

void solve(){
    int n, m;
    cin >> n >> m;

    int a[n + 1], outd[n + 1], ind[n + 1];
    LCA lca(n + 1);

    for(int i = 1; i <= n; i++){
        ind[i] = outd[i] = 0;
        cin >> a[i];
        h[i] = -1;
    }
    idx = 0;

    int f[n + 1];
    for(int i = 0; i <= n; i++){
        f[i] = (int)1e9;
    }

    for(int i = 0; i < m; i++){
        int u, v;
        cin >> u >> v;

        add(u, v);
        lca.add(u, v);
        ind[v]++, outd[u]++;
    }

    for(int i = 1; i <= n; i++){
        if(ind[i] == 0){
            lca.bfs(i);
        }
    }

    queue<int> q;
    for(int i = 1; i <= n; i++){
        if(ind[i] == 0){
            q.push(i);
            f[i] = a[i];
        }
    }

    while(!q.empty()){
        int u = q.front();
        q.pop();

        for(int i = h[u]; ~i; i = ne[i]){
            int v = e[i];

            f[v] = min(f[v], f[u] + a[v]);
            ind[v]--;
            if(ind[v] == 0){
                q.push(v);
            }
        }
    }

    int qwq = (int)1e9;
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++){
            if(i != j && outd[i] == 0 && outd[j] == 0){
                if(lca.lca(i, j) == 0){
                    qwq = chmin(qwq, f[i] + f[j]);
                }
                else {
                    qwq = chmin(qwq, f[i] + f[j] - f[lca.lca(i, j)]);
                }
            }
        }
    }
    cout << qwq << endl;
}

int main() {
    // freopen(".in", "r", stdin);
    // freopen(".out", "w", stdout);

    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int t = 1;
//    cin >> t;

    while(t--){
        solve();
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3564kb

input:

2 0
10 20

output:

30

result:

ok single line: '30'

Test #2:

score: 0
Accepted
time: 0ms
memory: 3552kb

input:

3 2
10 20 30
1 2
1 3

output:

60

result:

ok single line: '60'

Test #3:

score: 0
Accepted
time: 29ms
memory: 3788kb

input:

1000 999
589 300 181 203 350 691 812 963 333 957 894 681 630 24 774 397 768 215 403 868 995 248 156 160 528 940 862 965 889 535 939 782 453 935 641 733 864 681 364 225 661 198 305 778 17 901 408 208 134 555 316 57 128 821 307 882 519 302 5 421 194 348 491 912 820 156 797 254 374 70 175 95 497 315 17...

output:

1196

result:

ok single line: '1196'

Test #4:

score: 0
Accepted
time: 31ms
memory: 3712kb

input:

1000 999
400 323 358 806 874 176 813 905 306 813 441 195 671 190 924 733 252 22 892 289 561 545 821 490 830 87 255 104 998 956 935 832 211 316 476 435 484 260 777 133 8 932 618 189 485 800 548 509 570 54 662 783 373 601 406 463 394 36 937 160 368 835 371 486 185 139 255 947 311 898 443 581 990 105 4...

output:

1069

result:

ok single line: '1069'

Test #5:

score: 0
Accepted
time: 33ms
memory: 3708kb

input:

1000 999
432 313 769 817 653 951 97 56 224 152 283 857 315 377 289 578 124 748 599 926 535 19 722 684 533 13 669 783 166 646 546 961 951 488 106 824 423 320 25 298 724 291 961 501 437 552 449 791 419 943 169 836 425 910 269 982 309 741 92 79 914 303 492 234 891 689 147 695 614 799 596 501 363 570 47...

output:

1334

result:

ok single line: '1334'

Test #6:

score: 0
Accepted
time: 32ms
memory: 3808kb

input:

1000 999
599 62 219 707 844 743 690 882 240 569 198 276 812 26 213 688 353 880 154 474 891 927 892 472 869 158 559 496 674 484 978 557 813 56 564 280 350 318 683 602 218 53 59 743 981 61 615 815 359 702 727 823 88 403 518 628 305 254 694 519 345 677 882 852 696 498 80 429 348 184 570 515 277 177 104...

output:

1275

result:

ok single line: '1275'

Test #7:

score: 0
Accepted
time: 112ms
memory: 3716kb

input:

1000 0
376 610 318 581 85 805 725 305 931 572 790 288 642 350 740 398 716 222 577 544 750 766 176 549 221 989 670 17 870 591 951 911 534 943 997 458 384 291 699 496 917 780 152 60 773 937 512 638 3 599 388 745 327 23 640 569 918 300 805 287 52 279 229 891 393 785 716 381 192 628 31 136 156 918 844 4...

output:

3

result:

ok single line: '3'

Test #8:

score: 0
Accepted
time: 82ms
memory: 3720kb

input:

1000 196
543 842 44 237 511 790 282 553 481 697 246 362 659 655 607 222 51 380 743 468 834 601 643 659 824 711 697 398 753 151 538 525 188 851 134 779 996 485 415 872 911 962 326 34 331 786 250 909 833 758 79 564 706 518 682 810 831 623 147 570 973 62 187 623 644 36 173 49 776 949 721 309 72 286 339...

output:

3

result:

ok single line: '3'

Test #9:

score: 0
Accepted
time: 56ms
memory: 3768kb

input:

1000 419
608 725 693 959 612 919 561 776 862 856 761 638 640 703 798 798 520 986 774 927 688 796 623 523 631 966 614 608 887 971 856 616 846 850 747 686 809 881 718 530 960 908 906 729 887 634 605 807 999 732 700 794 561 921 593 796 504 626 872 820 938 860 636 622 736 738 907 590 871 961 642 827 940...

output:

1001

result:

ok single line: '1001'

Test #10:

score: 0
Accepted
time: 0ms
memory: 3532kb

input:

7 6
10 9 6 4 2 10 12
1 2
1 3
2 4
2 5
3 6
3 7

output:

25

result:

ok single line: '25'

Test #11:

score: 0
Accepted
time: 30ms
memory: 3940kb

input:

1000 885
543 757 983 811 867 900 922 917 530 553 507 548 962 644 773 888 800 558 627 750 913 580 862 941 522 976 707 923 914 564 883 827 901 754 762 753 806 703 942 854 951 761 633 602 777 623 730 964 855 611 903 865 800 940 503 549 705 861 715 708 949 571 937 633 995 903 812 894 778 936 748 839 581...

output:

1052

result:

ok single line: '1052'

Test #12:

score: 0
Accepted
time: 1ms
memory: 3552kb

input:

127 126
1
1 10
1 1 10 10
1 1 1 1 10 10 10 10
1 1 1 1 1 1 1 1 10 10 10 10 10 10 10 10
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10
1000 1000 1000 1000 1000 1000 1000 1000
1000 1000 1000 1000 1000 1000 1000 1000
1000 1000 1000 1000 1000 1000 1000 1000
1000 1000 1000 ...

output:

71

result:

ok single line: '71'

Test #13:

score: 0
Accepted
time: 1ms
memory: 3492kb

input:

127 126
1
1 10
1 1 10 10
1 1 1 1 10 10 10 10
1 1 1 1 1 1 1 1 10 10 10 10 10 10 10 10
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10
1000 1000 1000 1000 1000 1000 1000 1000
1000 1000 1000 1000 1000 1000 1000 1000
1000 1000 1000 1000 1000 1000 1000 1000
1000 1000 1000 ...

output:

71

result:

ok single line: '71'

Test #14:

score: 0
Accepted
time: 0ms
memory: 3592kb

input:

128 126
22 1 44 35 9 88 37 54 37 6 75 17 4 32 78 25 15 95 1 13 78 99 39 97 100 51 13 60 19 56 70 27 24 63 80 78 100 3 92 78 94 3 5 6 84 2 94 90 14 100 81 58 90 61 25 96 30 82 16 42 37 87 30 47 8 21 1 44 54 60 10 78 2 61 14 59 1 19 66 66 5 20 9 1 91 22 55 26 14 92 95 62 85 87 67 2 11 63 36 39 27 83 4...

output:

147

result:

ok single line: '147'

Test #15:

score: 0
Accepted
time: 1ms
memory: 3548kb

input:

101 100
1 76 1 44 1 80 1 82 1 64 1 45 1 27 1 84 1 87 1 97 1 89 1 15 1 77 1 30 1 11 1 70 1 75 1 36 1 30 1 76 1 95 1 99 1 63 1 47 1 12 1 78 1 53 1 38 1 68 1 14 1 86 1 97 1 10 1 62 1 58 1 63 1 23 1 11 1 76 1 18 1 25 1 93 1 90 1 73 1 93 1 29 1 33 1 92 1 16 1 1 1
1 2
1 3
3 4
3 5
5 6
5 7
7 8
7 9
9 10
9 11...

output:

41

result:

ok single line: '41'

Test #16:

score: 0
Accepted
time: 0ms
memory: 3568kb

input:

101 100
1 76 1 44 1 80 1 82 1 64 1 45 1 27 1 84 1 87 1 97 1 89 1 115 1 77 1 30 1 111 1 70 1 75 1 36 1 30 1 76 1 95 1 99 1 63 1 47 1 12 1 78 1 53 1 38 1 68 1 14 1 86 1 97 1 10 1 62 1 58 1 63 1 23 1 11 1 76 1 18 1 25 1 93 1 90 1 73 1 93 1 29 1 33 1 92 1 16 1 1 1
1 2
1 3
3 4
3 5
5 6
5 7
7 8
7 9
9 10
9 ...

output:

52

result:

ok single line: '52'

Test #17:

score: 0
Accepted
time: 112ms
memory: 3792kb

input:

1000 999
51 479 489 803 520 562 5 104 764 587 350 801 936 537 306 8 117 467 526 415 741 929 708 488 999 233 928 982 368 943 248 684 983 404 249 792 825 1 142 164 291 344 484 409 646 831 297 720 965 555 594 62 594 45 812 34 174 843 18 623 655 157 653 713 699 178 826 206 179 447 933 682 124 572 451 77...

output:

54

result:

ok single line: '54'

Test #18:

score: 0
Accepted
time: 91ms
memory: 3760kb

input:

931 930
628 697 759 925 980 702 663 487 896 759 90 449 94 654 846 202 492 225 441 927 819 28 279 285 927 793 622 259 627 976 637 150 953 92 251 522 423 165 996 954 925 67 221 229 291 957 601 85 920 142 884 325 757 376 436 54 191 464 112 238 843 806 921 750 173 744 797 6 865 482 554 479 226 795 615 2...

output:

725

result:

ok single line: '725'

Test #19:

score: 0
Accepted
time: 60ms
memory: 3936kb

input:

820 819
458 446 495 82 250 22 828 81 623 625 955 295 515 12 93 633 929 832 618 447 373 951 905 823 721 433 744 250 697 583 75 13 496 763 193 341 524 962 690 599 780 920 404 868 761 534 381 694 130 185 787 170 650 218 847 818 53 94 56 133 784 354 764 493 981 965 201 988 758 154 66 556 105 8 189 402 9...

output:

752

result:

ok single line: '752'

Test #20:

score: 0
Accepted
time: 44ms
memory: 3728kb

input:

781 780
142 27 975 196 634 262 219 1000 155 411 499 126 826 770 681 559 417 678 247 256 524 194 691 601 395 715 297 346 108 833 104 162 549 338 38 190 63 373 519 683 6 960 697 488 613 71 259 914 918 242 459 933 159 353 871 852 736 414 596 878 217 823 674 317 113 359 809 892 809 737 548 412 103 288 3...

output:

476

result:

ok single line: '476'

Test #21:

score: 0
Accepted
time: 0ms
memory: 3588kb

input:

4 2
10 7 4 6
1 4
2 3

output:

27

result:

ok single line: '27'

Test #22:

score: 0
Accepted
time: 0ms
memory: 3544kb

input:

7 6
10 9 6 10 2 4 12
1 2
1 3
2 4
2 5
3 6
3 7

output:

31

result:

ok single line: '31'

Test #23:

score: 0
Accepted
time: 30ms
memory: 3700kb

input:

1000 500
705 710 905 833 669 540 752 766 775 661 864 881 966 916 631 948 716 895 502 765 771 867 600 741 750 510 780 781 961 783 851 546 670 817 777 933 832 716 989 571 946 769 979 963 634 957 831 619 640 550 928 512 557 806 586 955 917 977 643 632 960 893 940 753 706 901 948 876 647 851 597 946 646...

output:

2036

result:

ok single line: '2036'

Test #24:

score: 0
Accepted
time: 32ms
memory: 3712kb

input:

951 762
598 518 890 596 587 509 645 591 971 829 509 851 935 716 693 686 930 587 747 540 840 733 585 976 577 535 811 774 500 616 577 584 670 996 806 688 538 729 789 985 512 801 537 966 926 791 700 997 527 584 729 551 706 990 978 925 856 577 869 787 602 530 559 553 569 997 944 780 749 839 938 578 945 ...

output:

1036

result:

ok single line: '1036'

Test #25:

score: 0
Accepted
time: 16ms
memory: 3740kb

input:

706 680
514 849 705 595 848 515 613 771 861 565 779 629 706 540 761 904 986 743 695 850 881 511 816 907 982 696 908 909 558 589 904 973 681 672 832 837 540 958 749 572 806 906 771 515 853 872 652 944 724 502 539 622 990 803 685 821 634 564 794 864 966 577 710 777 903 616 654 556 663 601 573 772 902 ...

output:

1228

result:

ok single line: '1228'

Test #26:

score: 0
Accepted
time: 13ms
memory: 3652kb

input:

615 580
901 815 763 737 564 676 767 510 894 902 703 528 860 604 955 946 957 584 965 925 552 522 641 568 953 930 506 572 944 982 991 599 539 512 767 767 658 921 806 695 590 760 812 919 540 681 976 855 869 729 714 930 645 783 683 948 743 698 538 653 684 891 891 501 605 646 659 650 620 516 839 808 864 ...

output:

1278

result:

ok single line: '1278'

Test #27:

score: 0
Accepted
time: 31ms
memory: 3968kb

input:

975 902
888 682 546 673 936 926 949 565 924 636 751 697 880 693 867 509 866 882 861 996 864 631 912 997 698 504 859 894 640 942 620 971 874 655 830 838 791 687 611 803 815 696 550 776 587 761 852 958 612 775 926 826 844 764 527 502 654 958 943 652 558 629 878 606 904 585 772 745 554 894 791 773 853 ...

output:

1246

result:

ok single line: '1246'

Test #28:

score: 0
Accepted
time: 19ms
memory: 3908kb

input:

788 719
977 592 550 912 683 850 890 917 561 757 865 802 517 762 915 924 597 966 670 731 523 910 812 837 774 894 573 909 930 902 501 818 804 769 521 727 746 904 886 884 982 800 808 731 886 788 822 708 588 521 630 682 711 723 634 568 506 757 979 993 725 706 582 749 599 560 789 912 620 542 956 720 819 ...

output:

1162

result:

ok single line: '1162'

Test #29:

score: 0
Accepted
time: 31ms
memory: 3716kb

input:

976 822
989 903 744 923 848 975 692 680 667 812 561 780 869 903 860 768 553 591 947 964 963 704 606 550 837 784 898 826 856 726 707 646 818 937 558 914 736 957 818 545 590 513 917 832 882 560 827 879 982 710 726 753 569 700 521 743 680 656 822 770 859 673 836 900 647 730 814 778 773 973 718 612 897 ...

output:

1006

result:

ok single line: '1006'

Test #30:

score: 0
Accepted
time: 14ms
memory: 3652kb

input:

662 598
812 685 695 685 737 948 583 992 570 546 649 548 579 612 938 704 635 943 628 508 536 553 891 560 547 531 527 505 878 678 872 818 728 838 610 580 991 703 649 526 761 644 624 693 797 997 886 520 946 588 773 664 671 852 771 999 621 943 603 567 878 679 646 814 942 788 737 956 755 764 894 540 540 ...

output:

1148

result:

ok single line: '1148'

Test #31:

score: 0
Accepted
time: 15ms
memory: 3728kb

input:

759 658
643 871 904 535 522 527 556 816 982 770 994 579 823 936 648 780 530 649 609 937 983 879 821 702 853 795 700 601 519 955 531 703 788 776 902 945 976 589 867 658 790 984 864 882 883 748 595 913 834 977 699 710 826 580 875 863 712 775 683 666 697 765 835 625 540 775 780 538 546 528 594 883 547 ...

output:

1052

result:

ok single line: '1052'

Test #32:

score: 0
Accepted
time: 12ms
memory: 3580kb

input:

597 488
609 980 826 518 721 545 693 762 619 887 688 510 855 803 761 882 562 813 826 992 637 728 754 781 836 646 860 806 632 937 678 712 782 940 967 520 516 570 870 888 991 970 607 640 989 942 529 660 630 522 626 720 965 610 538 746 995 816 742 951 810 517 706 925 613 950 719 645 642 811 855 994 703 ...

output:

1028

result:

ok single line: '1028'