QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#225618#3858. Systematic salesmanEnergy_is_not_over#AC ✓546ms129240kbC++174.8kb2023-10-24 21:09:082023-10-24 21:09:09

Judging History

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

  • [2023-10-24 21:09:09]
  • 评测
  • 测评结果:AC
  • 用时:546ms
  • 内存:129240kb
  • [2023-10-24 21:09:08]
  • 提交

answer

//
// Stvoreno Energom o 24.10.23. 14:22:07
//

#include <bits/stdc++.h>

#define F first
#define S second
#define MP make_pair
#define PB push_back

#define all(a) a.begin(), a.end()
#define len(a) (int) (a.size())
#define fir first
#define sec second
#define mp make_pair
#define pb push_back

using namespace std;

#ifdef Energy_is_not_over
#define DEBUG for (int _____DEBUG=1;_____DEBUG;_____DEBUG=0)
#define LOG(...) print(#__VA_ARGS__" ::",__VA_ARGS__)<<endl

template<class ...Ts>
auto &print(Ts ...ts) { return ((cerr << ts << " "), ...); }

#else
#define DEBUG while (0)
#define LOG(...)
#endif

typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;

const int max_n = 2000+10, inf = 1000111222;

int last=0;
int l[max_n];
int r[max_n];
int new_node()
{
    return ++last;
}
pair<pii,int> leaf[2*max_n];

double dist[max_n][max_n];

int build_tree(vector<pair<pii,int>> v,int type)
{
    int res=new_node();
    if (len(v)==1){
        LOG(res,v[0].fir.fir,v[0].fir.sec,v[0].sec);
        leaf[res]=v[0];
        return res;
    }

    if (type==0){
        sort(all(v),[&](const pair<pii,int>& lhs,const pair<pii,int>& rhs){
            return lhs.fir.fir<rhs.fir.fir;
        });
    }
    else{
        sort(all(v),[&](const pair<pii,int>& lhs,const pair<pii,int>& rhs){
            return lhs.fir.sec<rhs.fir.sec;
        });
    }
    l[res]=build_tree({v.begin(),v.begin()+len(v)/2},type^1);
    r[res]=build_tree({v.begin()+len(v)/2,v.end()},type^1);
    return res;
}

vector<int> all_down[max_n];
vector<pii> all_pairs[max_n];

double dp[max_n][max_n];
pair<int,int> dp_pred[max_n][max_n];
double pr_dp[max_n][max_n];
pair<int,int> pr_dp_pred[max_n][max_n];

void build_dp(int now)
{
    if (l[now]==0 && r[now]==0){
        all_down[now].pb(leaf[now].sec);
        all_pairs[now].pb(mp(leaf[now].sec,leaf[now].sec));
        dp[leaf[now].sec][leaf[now].sec]=0;
        return;
    }

    build_dp(l[now]);
    for (auto i:all_down[l[now]]){
        all_down[now].pb(i);
    }
    build_dp(r[now]);
    for (auto i:all_down[r[now]]){
        all_down[now].pb(i);
    }
    for (auto k1:all_down[l[now]]){
        for (auto k2:all_down[r[now]]){
            all_pairs[now].pb(mp(k1,k2));
            all_pairs[now].pb(mp(k2,k1));
        }
    }

    for (int iter=0;iter<2;iter++){
        for (auto [st1,fn1]:all_pairs[l[now]]){
            for (auto st2:all_down[r[now]]){
                if (pr_dp[st1][st2]>dp[st1][fn1]+dist[fn1][st2]){
                    pr_dp[st1][st2]=dp[st1][fn1]+dist[fn1][st2];
                    pr_dp_pred[st1][st2]=mp(fn1,iter);
                }
            }
        }
        for (auto st1:all_down[l[now]]){
            for (auto [st2,fn2]:all_pairs[r[now]]){
                if (dp[st1][fn2]>pr_dp[st1][st2]+dp[st2][fn2]){
                    dp[st1][fn2]=pr_dp[st1][st2]+dp[st2][fn2];
                    dp_pred[st1][fn2]=mp(st2,iter);
                }
            }
        }
        swap(l[now],r[now]);
    }
}

vector<int> ans;

void restore_ans(int now,pii wanted_pair)
{
    if (l[now]==0 && r[now]==0){
        ans.pb(leaf[now].sec);
        return;
    }
    if (wanted_pair==mp(-1,-1)){
        for (auto i:all_pairs[now]){
            if (wanted_pair==mp(-1,-1) || dp[i.fir][i.sec]<dp[wanted_pair.fir][wanted_pair.sec]){
                wanted_pair=i;
            }
        }
    }
    pii st2_and_iter=dp_pred[wanted_pair.fir][wanted_pair.sec];
    pii fn1_and_iter=pr_dp_pred[wanted_pair.fir][st2_and_iter.fir];
    if (st2_and_iter.sec){
        swap(l[now],r[now]);
    }
    restore_ans(l[now],mp(wanted_pair.fir,fn1_and_iter.fir));
    restore_ans(r[now],mp(st2_and_iter.fir,wanted_pair.sec));
}

int main() {
//    freopen("input_l.txt","r",stdin);
//    freopen("output.txt","w",stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    for (int i=0;i<max_n;i++){
        for (int j=0;j<max_n;j++){
            dp[i][j]=1e18;
            pr_dp[i][j]=1e18;
        }
    }

    int n;
    cin>>n;
    if (n==1){
        cout<<fixed<<setprecision(10)<<0<<"\n";
        cout<<1<<"\n";
        return 0;
    }
    vector<pair<pii,int>> xy(n);
    for (int i=0;i<n;i++){
        cin>>xy[i].fir.fir>>xy[i].fir.sec;
        xy[i].sec=i;
    }
    for (int i=0;i<n;i++){
        for (int j=0;j<n;j++){
            int dx=xy[i].fir.fir-xy[j].fir.fir;
            int dy=xy[i].fir.sec-xy[j].fir.sec;
            dist[i][j]=sqrtl(1ll*dx*dx+1ll*dy*dy);
        }
    }
    int root=build_tree(xy,0);
    build_dp(root);
    restore_ans(root,mp(-1,-1));
    ld res=0;
    for (int i=0;i+1<len(ans);i++){
        res+=dist[ans[i]][ans[i+1]];
    }
    cout<<fixed<<setprecision(10)<<res<<"\n";
    for (auto i:ans){
        cout<<i+1<<" ";
    }
    cout<<"\n";
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 4ms
memory: 73688kb

input:

8
7 3
2 0
4 5
1 4
8 2
9 9
0 8
6 1

output:

26.3833257716
7 3 4 2 8 5 1 6 

result:

ok correct!

Test #2:

score: 0
Accepted
time: 4ms
memory: 71440kb

input:

20
4 71
52 7
49 15
59 83
12 9
46 6
74 44
89 50
32 10
82 58
11 33
78 72
27 49
64 75
97 0
38 46
91 54
8 70
18 61
79 92

output:

374.8836811175
14 4 20 12 10 17 8 15 7 2 6 3 9 5 11 16 13 19 18 1 

result:

ok correct!

Test #3:

score: 0
Accepted
time: 7ms
memory: 74100kb

input:

100
192 64
839 68
846 688
911 133
110 439
592 226
355 364
418 487
402 466
436 425
509 847
542 78
648 404
954 313
726 906
777 922
596 550
159 172
507 651
720 932
575 805
889 193
246 206
175 326
897 464
108 70
790 2
548 624
867 222
743 269
41 98
348 173
49 915
35 939
404 571
371 625
363 758
317 155
90...

output:

8491.4521602340
31 26 86 1 98 18 93 79 5 45 90 24 23 38 42 57 32 56 85 49 67 7 10 9 41 63 83 62 96 47 39 33 34 48 72 91 60 61 88 11 92 80 99 37 89 36 35 8 82 19 75 28 44 17 71 78 55 97 69 50 58 21 15 20 81 16 74 66 76 87 3 46 54 77 95 68 73 25 52 43 14 84 4 22 29 40 2 27 100 53 12 65 6 30 64 51 13 7...

result:

ok correct!

Test #4:

score: 0
Accepted
time: 54ms
memory: 94556kb

input:

500
380 190
314 738
973 705
875 517
593 638
132 681
714 411
400 589
139 353
339 771
832 883
610 170
925 431
351 96
884 683
674 817
5 386
710 99
348 173
996 812
533 453
851 10
877 142
86 361
860 168
489 50
641 766
158 118
989 745
823 559
374 971
605 158
432 307
931 863
719 635
73 341
726 906
536 372
...

output:

22821.8889783555
268 48 322 189 168 140 264 178 400 430 112 8 162 407 355 111 329 269 472 242 123 195 102 361 311 478 245 413 491 452 261 434 397 460 151 344 485 240 193 489 218 391 464 97 342 494 145 31 373 190 471 403 169 251 385 422 382 395 10 50 155 2 461 60 199 126 236 91 401 276 324 222 328 31...

result:

ok correct!

Test #5:

score: 0
Accepted
time: 539ms
memory: 116900kb

input:

1000
818 537
491 340
916 881
776 67
368 978
888 853
8 349
561 929
604 8
349 828
874 894
257 757
667 962
242 746
3 614
931 863
235 578
516 580
61 177
13 821
949 165
231 732
970 21
711 731
392 374
878 672
106 596
82 166
149 539
944 485
481 675
845 636
352 185
326 4
229 472
617 972
622 175
83 554
447 7...

output:

32684.7826593013
915 327 292 156 727 752 92 917 577 264 965 899 484 902 831 648 44 562 708 797 869 271 508 949 762 146 453 837 253 115 369 620 822 208 551 773 419 236 260 880 395 860 885 312 599 789 431 756 958 130 166 405 348 598 226 86 257 592 154 135 279 103 334 922 302 765 646 758 337 889 416 71...

result:

ok correct!

Test #6:

score: 0
Accepted
time: 546ms
memory: 122808kb

input:

1000
94954 408313
589670 644618
101544 170845
308094 798263
871557 182716
42389 153936
777317 429523
812471 38482
979047 249000
967597 351300
982071 356744
369070 837238
661606 876392
70400 544589
460840 381840
151672 220775
539578 774105
717079 505259
241023 619236
318139 186353
39127 718711
697393...

output:

32281914.2524237324
325 372 208 79 234 190 602 359 664 336 351 480 44 875 296 19 606 76 900 126 535 309 425 941 800 938 511 324 980 485 704 898 14 512 963 524 830 643 316 583 401 612 724 559 675 411 732 331 115 388 123 746 21 876 568 834 182 82 136 58 94 874 206 949 804 787 109 478 229 197 97 911 59...

result:

ok correct!

Test #7:

score: 0
Accepted
time: 510ms
memory: 127220kb

input:

1000
1097 1097
1661 1661
1121 1121
1377 1377
1541 1541
1907 1907
1796 1796
1547 1547
1376 1376
1992 1992
1317 1317
1762 1762
1561 1561
1794 1794
1874 1874
1577 1577
1688 1688
1650 1650
1460 1460
1062 1062
1247 1247
1596 1596
1996 1996
1146 1146
1452 1452
1190 1190
1839 1839
1799 1799
1346 1346
1889 ...

output:

1412.7993488107
139 910 204 440 142 764 831 963 668 37 769 289 830 606 781 787 561 613 554 343 426 658 451 92 408 433 821 423 689 862 496 914 949 223 398 771 125 840 150 835 331 542 587 999 843 974 841 470 42 118 527 194 836 205 106 962 187 279 869 652 249 567 20 241 893 513 383 50 352 135 964 631 1...

result:

ok correct!

Test #8:

score: 0
Accepted
time: 534ms
memory: 129240kb

input:

1000
1640 360
1469 531
1967 33
1800 200
1807 193
1265 735
1178 822
1747 253
1327 673
1164 836
1188 812
1623 377
1684 316
1806 194
1577 423
1915 85
1380 620
1033 967
1510 490
1213 787
1363 637
1751 249
1944 56
1252 748
1044 956
1158 842
1484 516
1242 758
1991 9
1212 788
1446 554
1576 424
1683 317
127...

output:

1412.7993488107
691 968 983 575 70 748 324 137 934 588 993 560 379 512 474 885 81 333 54 966 574 874 720 341 609 823 501 174 144 918 441 185 577 18 618 816 158 917 323 724 234 537 854 728 25 791 491 308 278 253 42 374 847 650 152 923 462 985 315 378 822 295 614 108 203 371 205 989 110 243 898 403 51...

result:

ok correct!

Test #9:

score: 0
Accepted
time: 533ms
memory: 128396kb

input:

1000
921 1079
860 860
815 1185
821 1179
50 50
311 1689
244 244
788 788
508 508
934 934
845 1155
584 584
170 170
589 1411
605 1395
88 88
439 1561
593 1407
842 842
647 1353
64 64
93 1907
930 930
730 730
328 328
151 1849
354 354
599 1401
849 1151
457 1543
808 808
469 1531
119 1881
604 604
130 130
305 1...

output:

4400.5995467104
899 658 474 908 175 260 295 425 44 229 693 839 599 832 418 822 630 136 366 528 868 877 670 130 536 477 95 572 845 856 485 57 369 511 514 867 616 278 163 551 609 595 844 137 621 190 22 283 749 767 970 761 659 526 890 774 891 483 225 33 241 677 261 758 618 802 110 59 797 950 530 852 12...

result:

ok correct!

Test #10:

score: 0
Accepted
time: 540ms
memory: 126796kb

input:

1000
208821 93534
971563 333783
973615 339722
964813 684252
218789 913425
751635 932065
816127 887381
657687 25516
515910 253
381949 14136
648326 977493
348288 976428
993310 581520
284456 48845
42 493513
828139 877260
6188 421581
118647 823372
989248 603131
927245 240266
459001 998316
434015 995627
...

output:

3138446.5260464719
960 570 289 296 881 394 30 652 998 345 788 759 500 554 138 446 174 180 133 980 698 49 969 263 350 820 526 419 184 62 332 877 989 938 221 784 477 10 932 826 728 875 845 101 818 154 880 566 993 985 273 335 563 708 921 809 724 395 833 226 194 896 988 751 317 662 63 909 765 102 14 725...

result:

ok correct!